HarmonyOS 关于@Observed @ObjectLink监测嵌套属性不成功的问题

将父类型和子类型都添加了@Observed修饰,可是修改父类型数组中元素的嵌套对象属性变化时,界面没有变化。

HarmonyOS 关于@Observed @ObjectLink监测嵌套属性不成功的问题 -鸿蒙开发者社区

HarmonyOS
2025-01-09 15:09:28
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

数组类型的增减可以被UI框架观察到,但是数组内某一项的变量的值的修改不会被UI框架观察到。class类型的变量,是可以直接被UI框架观察到,和数组类型一样,class类型的属性的变量的值改变同样不会被UI框架观察到。

可参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#%E5%B5%8C%E5%A5%97%E5%AF%B9%E8%B1%A1

可参考以下代码:

@Entry
@Component
struct LinkDemo {
  @State itemList: ListItemModel[] = [
    new ListItemModel("title1", 1, new ListItemChildModel(1)),
    new ListItemModel("title2", 2, new ListItemChildModel(1))
  ];

  build() {
    Column({ space: 10 }) {
      List() {
        ForEach(this.itemList, (item: ListItemModel) => {
          ListItem() {
            ListItemView({ item: item })
          }
        })
      }
      .layoutWeight(1)
      Button() {
        Text('改变数组里第二个元素的属性')
      }
      .onClick(() => {
        this.itemList[1].count++
      })
      Button() {
        Text('改变数组里第二个元素子对象的属性')
      }
      .onClick(() => {
        this.itemList[1].changeFlag = !this.itemList[1].changeFlag
        this.itemList[1].inner.count++
      })
    }
  }
}

@Component
struct ListItemView {
  @ObjectLink item: ListItemModel;

  build() {
    Row({ space: 20 }) {
      Text(`count:${this.item.count}`)
      Button() {
        Text('改变对象属性')
      }
      .onClick(() => {
        this.item.count++
      })
      Text(`inner count:${this.item.inner.count}`)
      Button() {
        Text('改变内部对象属性')
      }
      .onClick(() => {
        this.item.changeFlag = !this.item.changeFlag
        this.item.inner.count++
      })
    }
  }
}

@Observed
class ListItemModel {
  public changeFlag: boolean = false
  public title: string
  public count: number
  public inner: ListItemChildModel

  constructor(title: string, count: number, inner: ListItemChildModel) {
    this.title = title;
    this.count = count;
    this.inner = inner;
  }
}


@Observed
class ListItemChildModel {
  public count: number

  constructor(count: number) {
    this.count = count;
  }
}
  • 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.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
分享
微博
QQ
微信
回复
2025-01-09 16:41:35
相关问题
HarmonyOS uris 使用不成功
1016浏览 • 1回复 待解决
ohpm unpublish 对于带group不成功
1469浏览 • 1回复 待解决
鸿蒙手表后台持续定位不成功
936浏览 • 0回复 待解决
HarmonyOS mqtt功能调试不成功
576浏览 • 1回复 待解决
HarmonyOS 接口调用不成功
897浏览 • 1回复 待解决
HarmonyOS lottie显示图片不成功
586浏览 • 1回复 待解决
HarmonyOS javaScriptProxy注入js不成功
647浏览 • 1回复 待解决
startAbility跳转不成功,错误码16000001
2665浏览 • 1回复 待解决
HarmonyOS ffmpeg静态库引用不成功
1040浏览 • 1回复 待解决
HarmonyOS skills 功能使用不成功
1020浏览 • 1回复 待解决
HarmonyOS 自定义相机拍照不成功
1004浏览 • 1回复 待解决
配置OHPM代理一直不成功
1423浏览 • 1回复 待解决
数据库插入数据不成功怎么回事?
3219浏览 • 1回复 待解决