HarmonyOS @LocalStorageProp是否可以触发@Watch

HarmonyOS
2024-12-25 14:26:22
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

@LocalStorageProp是可以触发@Watch的,参考示例如下:

let para: Record<string, number> = { 'PropA': 47 };
let storage: LocalStorage = new LocalStorage(para);

@Entry(storage)
@Component
struct Index {
  @State message: string = 'Hello World';
  // @LocalStorageProp变量装饰器与LocalStorage中的'PropA'属性建立单向绑定
  @LocalStorageProp('PropA') @Watch('onCountUpdated') storageProp1: number = 1;

  // total: number = 0;

  onCountUpdated(propName: string): void {
    // ++this.total;
    console.log('CompA onCountUpdated total:')
  }

  build() {
    RelativeContainer() {
      Column({ space: 15 }) {
        // 点击后从47开始加1,只改变当前组件显示的storageProp1,不会同步到LocalStorage中
        Button(`Parent from LocalStorage ${this.storageProp1}`)
          .onClick(() => {
            this.storageProp1 += 1
            console.log('CompA onCountUpdated this.storageProp1:' + this.storageProp1)
          })
        // Child()
      }
    }
    .height('100%')
    .width('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.
分享
微博
QQ
微信
回复
2024-12-25 15:59:19
相关问题
@Watch是否有粘性的概念
2603浏览 • 1回复 待解决
HarmonyOS @Watch无效
662浏览 • 1回复 待解决
Watch GT2什么时候可以升级鸿蒙
8399浏览 • 1回复 待解决
HarmonyOS @Watch函数调用问题
1034浏览 • 1回复 待解决
HarmonyOS Watch没有回调
821浏览 • 1回复 待解决
HarmonyOS watch不能监听provide
521浏览 • 1回复 待解决
HarmonyOS 关于@Watch监听状态问题
1421浏览 • 1回复 待解决
HarmonyOS @Watch回调方法没有改变
640浏览 • 1回复 待解决
HarmonyOS 使用@Watch观察值变化问题
632浏览 • 1回复 待解决