HarmonyOS @Watch可以观察到@Consume装饰的状态变量更改吗

@Watch可以观察到@Consume装饰的状态变量更改吗

HarmonyOS
2024-12-20 16:49:44
710浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

参考demo:

@Component
struct CompD {
  @Consume @Watch('onChange') selectedDate: Date;

  onChange() {
    console.info("值改变了!!!")
  }

  build() {
    Column() {
      Button(`child increase the day by 1`)
        .onClick(() => {
          this.selectedDate.setDate(this.selectedDate.getDate() + 1)
        })
      Button('child update the new date')
        .margin(10)
        .onClick(() => {
          this.selectedDate = new Date('2023-09-09')
        })
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2100-1-1'),
        selected: this.selectedDate
      })
    }
  }
}

@Entry
@Component
struct CompA {
  @Provide selectedDate: Date = new Date('2021-08-08')

  build() {
    Column() {
      Button('parent increase the day by 1')
        .margin(10)
        .onClick(() => {
          this.selectedDate.setDate(this.selectedDate.getDate() + 1)
        })
      Button('parent update the new date')
        .margin(10)
        .onClick(() => {
          this.selectedDate = new Date('2023-07-07')
        })
      DatePicker({
        start: new Date('1970-1-1'),
        end: new Date('2100-1-1'),
        selected: this.selectedDate
      })
      CompD()
    }
  }
}
  • 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.
分享
微博
QQ
微信
回复
2024-12-20 18:41:40
相关问题
HarmonyOS constraintSize支持状态变量
983浏览 • 1回复 待解决
HarmonyOS taskpool参数是状态变量crash
712浏览 • 1回复 待解决
HarmonyOS 状态变量不刷新问题
1630浏览 • 1回复 待解决
状态变量和常规变量有什么区别?
1293浏览 • 2回复 待解决
ArkTS中如何监听状态变量变化?
2025浏览 • 1回复 待解决
关于状态变量@state必须知道
2238浏览 • 1回复 待解决