对于多层嵌套的情况,比如二维数组,或者数组项class,或者class的属性是class,第二层的属性变化如何观察

对于多层嵌套的情况,比如二维数组,或者数组项class,或者class的属性是class,他们的第二层的属性变化如何去观察?

HarmonyOS
2024-08-06 13:05:49
1183浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
踮脚在树梢上

使用@Observed/@ObjectLink装饰器进行深层次的观察@Observed

class DateClass extends Date {
constructor(args: number | string) {
super(args)
}
}

@Observed
class ClassB {
public a: DateClass;

constructor(a: DateClass) {
this.a = a;
}
}

@Component
struct ViewA {
label: string = 'date';
@ObjectLink a: DateClass;

build() {
Column() {
Button(`child increase the day by 1`)
.onClick(() => {
this.a.setDate(this.a.getDate() + 1);
})
DatePicker({
start: new Date('1970-1-1'),
end: new Date('2100-1-1'),
selected: this.a
})
}
}
}

@Entry
@Component
struct ViewB {
@State b: ClassB = new ClassB(new DateClass('2023-1-1'));

build() {
Column() {
ViewA({ label: 'date', a: this.b.a })

Button(`parent update the new date`)
.onClick(() => {
this.b.a = new DateClass('2023-07-07');
})
Button(`ViewB: this.b = new ClassB(new DateClass('2023-08-20'))`)
.onClick(() => {
this.b = new ClassB(new DateClass('2023-08-20'));
})
}
}
}
  • 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.
分享
微博
QQ
微信
回复
2024-08-06 19:23:21


相关问题
class次刷新渲染数组
1183浏览 • 1回复 待解决
嵌套Class属性变化无法触发UI渲染
725浏览 • 1回复 待解决
HarmonyOS 二维数组刷新问题
743浏览 • 1回复 待解决
HarmonyOS 嵌套Class状态观察问题
278浏览 • 1回复 待解决
HarmonyOS 对于class如何实现多态
333浏览 • 1回复 待解决
HarmonyOS 一个class传递属性崩溃
426浏览 • 1回复 待解决
如何监听数组内对象属性变化
2768浏览 • 1回复 待解决
如何将JSON字符串转Class对象数组
897浏览 • 1回复 待解决