LocalStorageLink修饰的变量会自动保存

LocalStorageLink修饰的变量是否会自动保存?

@Component 
struct AAA { 
  @State storeLink1: number = 1; 
  @LocalStorageLink('PropA') storeLink2: number = 1; 
 
  build() { 
    Column() { 
      Text(this.storeLink1 + "") 
        .fontSize(30) 
        .width("100%") 
        .fontWeight(FontWeight.Bold) 
        .onClick(() => { 
          this.storeLink1++; 
        }) 
        .textAlign(TextAlign.Center) 
        .backgroundColor(Color.Blue) 
 
      Text(this.storeLink2 + "") 
        .fontSize(30) 
        .fontWeight(FontWeight.Bold) 
        .width("100%") 
        .textAlign(TextAlign.Center) 
        .onClick(() => { 
          this.storeLink2++; 
        }) 
        .backgroundColor(Color.Yellow) 
    } 
    .backgroundColor(Color.Red) 
    .width('100%') 
    .height(100) 
  } 
} 
 
 
@Entry 
@Component 
struct LocalStorageLinkTestPage { 
  @State showComponent: boolean = true; 
  @State readProp: string | undefined = "" 
 
  build() { 
    Column({ space: 20 }) { 
      Text("显示/移除") 
        .fontSize(30) 
        .width("100%") 
        .textAlign(TextAlign.Center) 
        .fontWeight(FontWeight.Bold) 
        .onClick(() => { 
          this.showComponent = !this.showComponent; 
        }) 
      if (this.showComponent) { 
        AAA(); 
      } 
    } 
    .height('100%') 
  } 
}
  • 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.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.

如上代码:如果LocalStorageLink修饰的变量storeLink2增加了,当移除掉这个Component,然后再添加回来,发现LocalStorageLink修饰的变量storeLink2还是之前的变量,比较疑问,LocalStorageLink修饰的变量是会自动保存到LocalStorage里面吗?

HarmonyOS
2024-06-03 23:56:03
1.2w浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
唯你而画

LocalStorageLink装饰的变量事件上是和LocalStorage实例存储的变量是同步的代码中并没有看的实例化LocalStorage可以在当前ets文件中创建LocalStorage实例再传入到entry页面,这个时候LocalStorageLink装饰的变量才和LocalStorage对应。或者在加载页面之前,在enrtyAbility中创建实例,通过windowStage加载页面的时候传入。当不手动创建传入LocalStorage而使用LocalStorageLink装饰变量的时候,会默认创建一个LocalStorage。

示例代码:

//方式一: 
@Component 
struct AAA { 
  @State storeLink1: number = 1; 
  @LocalStorageLink('PropA') storeLink2: number = 1; 
 
  build() { 
    Column() { 
      Text(this.storeLink1 + "") 
        .fontSize(30) 
        .width("100%") 
        .fontWeight(FontWeight.Bold) 
        .onClick(() => { 
          this.storeLink1++; 
        }) 
        .textAlign(TextAlign.Center) 
        .backgroundColor(Color.Blue) 
 
      Text(this.storeLink2 + "") 
        .fontSize(30) 
        .fontWeight(FontWeight.Bold) 
        .width("100%") 
        .textAlign(TextAlign.Center) 
        .onClick(() => { 
          this.storeLink2++; 
        }) 
        .backgroundColor(Color.Yellow) 
    } 
    .backgroundColor(Color.Red) 
    .width('100%') 
    .height(100) 
  } 
} 
let para: Record<string,number> = { 'PropA': 47 }; 
let storage: LocalStorage = new LocalStorage(para) 
 
@Entry(storage) 
@Component 
struct LocalStorageLinkTestPage { 
  @State showComponent: boolean = true; 
  @State readProp: string | undefined = "" 
 
  build() { 
    Column({ space: 20 }) { 
      Text("显示/移除") 
        .fontSize(30) 
        .width("100%") 
        .textAlign(TextAlign.Center) 
        .fontWeight(FontWeight.Bold) 
        .onClick(() => { 
          this.showComponent = !this.showComponent; 
        }) 
      if (this.showComponent) { 
        AAA(); 
      } 
    } 
    .height('100%') 
  } 
  aboutToAppear(){ 
 
  } 
} 
//方式二: 
//伙伴原有代码不变 
//在onWindowStageCreate中更改为 
onWindowStageCreate(windowStage: window.WindowStage): void { 
  // Main window is created, set main page for this ability 
  hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
  let para: Record<string,number> = { 'PropA': 47 }; 
let storage: LocalStorage = new LocalStorage(para) 
windowStage.loadContent('pages/Page6',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) ?? ''); 
}); 
}
  • 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.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
分享
微博
QQ
微信
回复
2024-06-04 22:44:39


相关问题
@LocalStorageLink修饰
1121浏览 • 1回复 待解决
var能否修饰ArkTS中变量
883浏览 • 1回复 待解决
@State 修饰变量值改变,界面不刷新
2151浏览 • 1回复 待解决
HarmonyOS 自动保存账号密码
509浏览 • 1回复 待解决
保存自动格式化代码如何配置
890浏览 • 1回复 待解决
HarmonyOS 自动签名导致appid变化
486浏览 • 1回复 待解决