文章目录
前言
这两天没有怎么更新文章,家里有点事情给耽误了近1天的时间,剩下的时间,我在进行元服务的开发中了,基本上已经开发结束了。又是一个里程碑式的进展。奥利给~
主要讲一下开发元服务中使用的知识和遇到的问题。
一、元服务开发中用到的知识
1.跳转页面并传参数
- A页面跳转到B页面,在A页面使用pushUrl写上要跳转的语法。
this.getUIContext().getRouter().pushUrl({
url: 'pages/BPage',
params:{
type:type.toString()
}
})
- 接收页面B,通过路由栈获取参数。在aboutToAppear页面即将出现时,获取数据。
/// 定义接收参数的格式
interface ParamsType {
type: string;
}
@Entry
@Component
export struct AddPage {
// 接收路由参数
@State params: ParamsType = { type: '0' };
aboutToAppear(): void {
// 通过路由栈获取参数
this.params = this.getUIContext().getRouter().getParams() as ParamsType;
this.message = this.params.type;
}
...
2.展示日期组件
/// 显示日历组件
private showActionSheet() {
this.getUIContext().showDatePickerDialog( {
lunar:false,
selected: new Date(),
lunarSwitch: false,
showTime: false,
shadow: {
radius: 10,
offsetX: 20,
offsetY: 20,
color: "#66333333"
},
onDateChange:(date)=>{
// 使用状态组件接收Date数据,改变时更新Text数据。
this.curPikerDate = date;
},
onAccept: (date) => {
// 如果不选默认显示当天
this.expiryDate = this.curPikerDate ?? new Date();
}
})
}
3.使用系统Toast组件
/// 显示系统Toast
showToast(message:string,failTip ?: String){
let promptAction: PromptAction = this.getUIContext().getPromptAction();
try {
promptAction.showToast({
message: message,
duration: 2000
});
} catch (error) {
let code = -1;
console.error(`showToast args error code is ${code}, message is ${failTip ?? message}`);
};
}
Toast展示如下图。
二、元服开发中遇到的问题
1.元服务中本地化数据,不支持数据库存储。本来打算用数据库进行存数据,后面增、删、改、查方便一些。发现竟然容不了。存储用的是@ohos.data.preferences (用户首选项)
三.小小展示一下
因为还没上架,可能还有小优化一下,我这里只展示一下首页吧!