HarmonyOS NavDestination中使用LocalStorage无效的问题?

项目中目前使用Navigation来做跳转。目前的问题是,用navigation打开的页面使用LocalStorage无效。

阅读 586
1 个回答

如果 用localStorage的话,@state修饰的关键词值的变化会导致页面跟着变化:

let para: Record<string, number> = { 'PropA': 47 };
let storage: LocalStorage = new LocalStorage(para);
@Entry(storage)
@Component
export default struct APage {
  @Consume('navigation') navigation: NavPathStack;
  @LocalStorageLink('PropA') storageLink: number =0;
  @State testid:number =0
  number: number = 0

  build() {
    NavDestination() {
      Column() {
        Text(JSON.stringify(this.testid))
          .fontSize("20vp")
          .textAlign(TextAlign.Center)
          .width('100%')
          .height("20vp")
          .onClick(() => {
            this.navigation.pushPath({
              name: "BPage"
            }, false);
          })

        Text("点击改变testId")
          .fontSize("20vp")
          .textAlign(TextAlign.Center)
          .width('100%')
          .height("20vp")
          .margin({top:'20vp'})
          .onClick(() => {
            this.number++
            storage.set('PropA', this.number)
            this.testid =  storage.get('PropA') as number
          })
      }
      .width("100%")
      .height('100%')
    }
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进