posts - 609,  comments - 13,  views - 64万
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V2/arkts-localstorage-0000001524537149-V2#section13961833151713
此处代码只精简了需要的
复制代码
import window from '@ohos.window';
export default class EntryAbility extends UIAbility {
  //共享 LocalStorage
  para: Record<string, number> = {
    'PropA': 47
  };
  storage: LocalStorage = new LocalStorage(this.para);
  onWindowStageCreate(windowStage: window.WindowStage) {
    //共享 LocalStorage
    windowStage.loadContent('pages/Index', this.storage, (err, data) => {
      if (err.code) {
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
        return;
      }
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
    });
  }
}
复制代码

pages/Index页面中使用,其他页面中使用也是一样的方式

复制代码
let storage = LocalStorage.GetShared()

@Entry(storage)
@Component
struct Index {
  // @LocalStorageLink变量装饰器与LocalStorage中的'PropA'属性建立双向绑定
  @LocalStorageLink('PropA') storeLinkPropA: number = 1;
  build() {
    Column() {
      Text("storeLinkPropA:" + this.storeLinkPropA.toString()).onClick(()=>{
        this.storeLinkPropA += 1;
      })
    }
  }
}
复制代码

 

posted on   邢帅杰  阅读(101)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
历史上的今天:
2018-06-06 观察者模式
点击右上角即可分享
微信分享提示