HarmonyOS 关于@Watch监听状态问题

示例代码如下:

@State @Watch('attrChange')  
attr: Attr = new Attr(new Start())  
attrChange() {  
  console.log('attr has changed:' + JSON.stringify(this.attr))  
}  
build() {  
  Column() {  
    Button('test').onClick((event: ClickEvent) => {  
      console.log('000:' + JSON.stringify(this.attr))  
      new Action().do(this.attr)  
      console.log('111:' + JSON.stringify(this.attr))  
    })  
  }  
  .height('100%')  
  .width('100%')  
}  
class Action {  
  do(attr: Attr) {  
    const newStart = new Start()  
    newStart.info = {  
      'title': 'fadshf'  
    }  
    attr.start = newStart  
  }  
}  
class Attr {  
  name: string = 'kac'  
  age: number = 18  
  info: Record<string, string> = {  
    'title': 'www',  
    'msg': 'qqq',  
  }  
  start: Start  
  constructor(start: Start) {  
    this.start = start  
  }  
}  
@Observed  
class Start {  
  road: string = 'ttt'  
  color: string = 'yellow'  
  info: Record<string, string> = {  
    'aaaa': '1111',  
    'bbbb': '22222'  
  }  
}

通过new Action().do(this.attr)将@State修饰的this.attr变量传入业务代码中,并改变其值,可以看到在'000','111'的log有打印变化,但是在attrChange()没打印log。而不通过new Action().do()改变值,直接在button的点击事件里改变this.attr中的start变量是可以触发attrChange()打印日志的。@State修饰的变量是否不支持传入业务代码进行处理?还是@Watch不支持观测复杂对象的属性变化?

HarmonyOS
2024-10-14 09:51:19
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

@Watch不支持观测嵌套对象的属性变化。

分享
微博
QQ
微信
回复
2024-10-14 17:44:18
相关问题
HarmonyOS watch不能监听provide
700浏览 • 1回复 待解决
HarmonyOS @Watch函数调用问题
1295浏览 • 1回复 待解决
HarmonyOS 关于ArkUI中状态栏的问题
1283浏览 • 1回复 待解决
在自定义组件里使用watch监听
1381浏览 • 1回复 待解决
HarmonyOS 关于状态管理
1313浏览 • 1回复 待解决
HarmonyOS 使用@Watch观察值变化问题
839浏览 • 1回复 待解决
HarmonyOS 如何监听折叠/展开状态
1049浏览 • 1回复 待解决
HarmonyOS 关于侧滑返回的监听
770浏览 • 1回复 待解决
HarmonyOS 监听电池电量及电池状态
1067浏览 • 1回复 待解决
HarmonyOS 如何监听折叠屏折叠状态变化
1492浏览 • 1回复 待解决
怎么监听蓝牙的开启状态
5824浏览 • 1回复 待解决
HarmonyOS @Watch无效
943浏览 • 1回复 待解决
HarmonyOS 扬声器开关状态如何监听
865浏览 • 1回复 待解决