使用@Watch回调来监测状态变量的变化。如果回调函数执行,说明在下一次VSync信号发送时,使用该状态变量的UI会刷新绘制。
参考代码如下:
@Component
struct TotalView {
@Prop @Watch('onCountUpdated') count: number = 0;
@State total: number = 0;
// @Watch callback
onCountUpdated(propName: string): void {
this.total += this.count;
}
build() {
Text(`Total: ${this.total}`)
}
}
@Entry
@Component
struct CountModifier {
@State count: number = 0;
build() {
Column() {
Button('add to basket')
.onClick(() => {
this.count++;
})
TotalView({ count: this.count })
}
}
} 参考链接