OpenHarmony开发者论坛
标题:
数组中的对象,必须用子组件才能检测到更新吗?
[打印本页]
作者:
Lewis
时间:
2023-11-24 15:59
提示:
作者被禁止或删除 内容自动屏蔽
作者:
马迪
时间:
2023-11-25 08:58
@Observed
class MyListData{
id:Number
name:string
constructor(a: MyListData) {
this.id = a.id;
this.name = a.name;
}
}
@Component
struct ViewA {
@ObjectLink a: MyListData;
build() {
Row() {
Text(this.a.name).fontSize(50)
.fontWeight(FontWeight.Bold)
}
}
}
@Entry
@Component
struct Index {
@State message: string = 'Hello World'
@State arr: MyListData[] = [new MyListData({id:0,name:"aaa"})]
build() {
Row() {
Column() {
Text(this.arr[0].name)
.fontSize(50)
.fontWeight(FontWeight.Bold)
ViewA({a:this.arr[0]})
}
.width('100%')
}
.height('100%').onClick(()=>{
// this.arr[0].name = "Curry" //仅子组件生效
this.arr[0] =new MyListData({id:0,name:"Curry"}) //重新new一个,子组件和当前组件都生效
})
}
}
复制代码
欢迎光临 OpenHarmony开发者论坛 (https://forums.openharmony.cn/)
Powered by Discuz! X3.5