• Lv0
    粉丝0

积分7 / 贡献0

提问7答案被采纳0文章0

数组中的对象,必须用子组件才能检测到更新吗?

来自手机 显示全部楼层 发表于 2023-11-24 15:59:34
提示: 作者被禁止或删除 内容自动屏蔽
您尚未登录,无法参与评论,登录后可以:
参与开源共建问题交流
认同或收藏高质量问答
获取积分成为开源共建先驱

精彩评论1

马迪

沙发 发表于 2023-11-25 08:58:09
  1. @Observed
  2. class MyListData{
  3.   id:Number
  4.   name:string

  5.   constructor(a: MyListData) {
  6.     this.id = a.id;
  7.     this.name = a.name;
  8.   }
  9. }

  10. @Component
  11. struct ViewA {
  12.   @ObjectLink a: MyListData;

  13.   build() {
  14.     Row() {
  15.       Text(this.a.name).fontSize(50)
  16.         .fontWeight(FontWeight.Bold)
  17.     }
  18.   }
  19. }

  20. @Entry
  21. @Component
  22. struct Index {
  23.   @State message: string = 'Hello World'
  24.   @State arr: MyListData[] = [new MyListData({id:0,name:"aaa"})]

  25.   build() {
  26.     Row() {
  27.       Column() {
  28.         Text(this.arr[0].name)
  29.           .fontSize(50)
  30.           .fontWeight(FontWeight.Bold)
  31.         ViewA({a:this.arr[0]})
  32.       }
  33.       .width('100%')
  34.     }
  35.     .height('100%').onClick(()=>{
  36.       // this.arr[0].name = "Curry"  //仅子组件生效
  37.       this.arr[0] =new MyListData({id:0,name:"Curry"}) //重新new一个,子组件和当前组件都生效
  38.     })
  39.   }
  40. }
复制代码

Copyright   ©2023  OpenHarmony开发者论坛  京ICP备2020036654号-3 |技术支持 Discuz!

返回顶部