Harmony笔记
Navigation界面跳转
@Entry
@Component
struct Index {
pageStack: NavPathStack = new NavPathStack();
build() {
Navigation(this.pageStack) {
Text("Hello")
.onClick(() => {
this.pageStack.pushPath({ name: "page2" })
})
}
.mode(NavigationMode.Stack)
.navDestination(PagesMap)
.hideToolBar(true)
.hideBackButton(true)
.hideTitleBar(true)
.hideNavBar(false)
}
}
@Builder
function PagesMap(name: string, param: object): void {
if (name == "page2") {
Page2Builder()
}
}
//Page2
@Builder
export function Page2Builder() {
Page2()
}
@Entry
struct Page2 {
build() {
NavDestination() {
Text("page2")
.onClick(() => {
this.getUIContext().getRouter().pushUrl({ url: "pages/Page3" })
})
}
.width('100%')
.height('100%')
.hideTitleBar(true)
.hideToolBar(true)
.hideBackButton(true)
}
}
PagesMap需要添加 @Builder注解,否则跳转不报错,但显示的是空白界面。
Router
-
Router的跳转目的地必须是Page,而NavDestination是Navigation的子组件。Page使用@Entry、@Component进行注解,并在resources\base\profile\main_page.json 中进行注册
@Entry @Component struct Page4 { build() { Text("page4") .id('Page3HelloWorld') .fontSize($r('app.float.page_text_font_size')) .fontWeight(FontWeight.Bold) .backgroundColor("red") .height('100%') .width('100%') } } //main_page.json { "src": [ "pages/Index", "pages/Page3", "pages/Page4" ] } -
Router传递参数时,如果直接传递数字,字符串,枚举值,如this.getUIContext().getRouter().pushUrl({ url: "pages/account/LoginPage", params: "2" });获取会得到undefined。
RdbStore
-
Parameter error.ValuesBucket is invalid.
let value = new Array<relationalStore.ValuesBucket>(); await db.batchInsert("news", value);如果value没有数据,执行时会报异常{"code":"401"}。
-
插入、更新数据 问题
//推荐显式使用ValuesBucket定义数据, const data: ValuesBucket = { groupName: groupName, groupID: id }; db.insert("groupinfo", data); //不推荐使用 db.insert("groupinfo", { groupName: groupName, groupID: id });显式的使用ValuesBucket定义数据,方便DevEco Studio的混淆助手自动识别添加字段到混淆白名单中,第二个方案会导致混淆助手无法识别添加白名单,groupName,groupID被混淆,导致插入或者更新数据出错。
-
同一个数据库对象,多个RdbStore实例对象的使用
let rdbStore1 = await relationalStore.getRdbStore(context, { name: "test.db", securityLevel: relationalStore.SecurityLevel.S4 }); let rdbStore2 = await relationalStore.getRdbStore(context, { name: "test.db", securityLevel: relationalStore.SecurityLevel.S4 }); await rdbStore1.close(); await delayFunc(3000); const t = new relationalStore.RdbPredicates("person") let r = await rdbStore2.query(t); //对于同一个test.db数据库,rdbStore1实例调用close()关闭,不会影响实例rdbStore2的操作。