参考: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; }) } } }