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%') 
  } 
}

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

HarmonyOS
2024-06-03 23:56:03
浏览
收藏 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) ?? ''); 
}); 
}
分享
微博
QQ
微信
回复
2024-06-04 22:44:39
相关问题
@LocalStorageLink修饰
772浏览 • 1回复 待解决
var能否修饰ArkTS中变量
498浏览 • 1回复 待解决
@State 修饰变量值改变,界面不刷新
1515浏览 • 1回复 待解决
保存自动格式化代码如何配置
462浏览 • 1回复 待解决
js相机组件拍照后自动保存
4162浏览 • 1回复 待解决