鸿蒙学习笔记
第九节 变量监听方法
当我们对一个变量增加了@Watch(xx)的方法的时候,
当这个变量的值改变的时候就会调用xx方法
当然这个xx方法需要你自己实现,并且不能在xx方法里面
修改自己的值,否则会死循环
全代码:
@Entry
@Component
// Watch 里面的监听方法不能去修改监听变量的值,否则会死循环
struct WatchStudy {
@State message: string = 'WatchStudy'
@State @Watch("updateAction") number1 : number = 1
@State @Watch("updateAction") number2 : number = 2;
@State val : number = 3;
updateAction(){
// this.number1 = this.number1 + 1
this.val = this.number1 * this.number2;
}
build() {
Row() {
Column() {
Text(""+this.number1)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
this.number1++
})
Divider()
Text(""+this.number2)
.fontSize(50)
.fontWeight(FontWeight.Bold)
.onClick(()=>{
this.number2+=2
})
Text(""+this.val)
.fontSize(50)
.fontWeight(FontWeight.Bold)
}
.width('100%')
}
.height('100%')
}
}
// @Watch 对状态数据进行监听,监听数据的变化 例如:有点像vue 数据监听