装饰的类能观察到第三层属性变化吗?this.a.b.c,c的变化?

装饰的类能观察到第三层属性变化吗,this.a.b.c,c的变化?

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

@ObjectLink装饰的成员变量仅能监听到其子属性的变化,再深入嵌套的属性便无法观测到了,因此我们只能改变它的子属性去通知对应组件重新渲染。

class BasicDataSource implements IDataSource {  
  private listeners: DataChangeListener[] = [];  
  private originDataArray: StringData[] = [];  
  
  public totalCount(): number {  
    return 0;  
  }  
  
  public getData(index: number): StringData {  
    return this.originDataArray[index];  
  }  
  
  registerDataChangeListener(listener: DataChangeListener): void {  
    if (this.listeners.indexOf(listener) < 0) {  
      console.info('add listener');  
      this.listeners.push(listener);  
    }  
  }  
  
  unregisterDataChangeListener(listener: DataChangeListener): void {  
    const pos = this.listeners.indexOf(listener);  
    if (pos >= 0) {  
      console.info('remove listener');  
      this.listeners.splice(pos, 1);  
    }  
  }  
  
  notifyDataReload(): void {  
    this.listeners.forEach(listener => {  
      listener.onDataReloaded();  
    })  
  }  
  
  notifyDataAdd(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataAdd(index);  
    })  
  }  
  
  notifyDataChange(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataChange(index);  
    })  
  }  
  
  notifyDataDelete(index: number): void {  
    this.listeners.forEach(listener => {  
      listener.onDataDelete(index);  
    })  
  }  
}  
  
class MyDataSource extends BasicDataSource {  
  private dataArray: StringData[] = [];  
  
  public totalCount(): number {  
    return this.dataArray.length;  
  }  
  
  public getData(index: number): StringData {  
    return this.dataArray[index];  
  }  
  
  public addData(index: number, data: StringData): void {  
    this.dataArray.splice(index, 0, data);  
    this.notifyDataAdd(index);  
  }  
  
  public pushData(data: StringData): void {  
    this.dataArray.push(data);  
    this.notifyDataAdd(this.dataArray.length - 1);  
  }  
}  
  
@Observed  
class StringData {  
  message: NestedString;  
  constructor(message: NestedString) {  
    this.message = message;  
  }  
}  
  
@Observed  
class NestedString {  
  message: string;  
  constructor(message: string) {  
    this.message = message;  
  }  
}  
  
@Entry  
@Component  
struct MyComponent {  
  private moved: number[] = [];  
  @State data: MyDataSource = new MyDataSource();  
  
  aboutToAppear() {  
    for (let i = 0; i <= 20; i++) {  
      this.data.pushData(new StringData(new NestedString(`Hello ${i}`)));  
    }  
  }  
  
  build() {  
    List({ space: 3 }) {  
      LazyForEach(this.data, (item: StringData, index: number) => {  
        ListItem() {  
          ChildComponent({data: item})  
        }  
        .onClick(() => {  
          item.message = new NestedString(item.message.message + '0');  
        })  
      }, (item: StringData, index: number) => item.toString() + index.toString())  
    }.cachedCount(5)  
  }  
}  
  
@Component  
struct ChildComponent {  
  @ObjectLink data: StringData  
  build() {  
    Row() {  
      Text(this.data.message.message).fontSize(50)  
        .onAppear(() => {  
          console.info("appear:" + this.data.message.message)  
        })  
    }.margin({ left: 10, right: 10 })  
  }  
}
分享
微博
QQ
微信
回复
2024-10-14 15:51:50
相关问题
arkui三层结构分别是什么?
285浏览 • 1回复 待解决
c/c++主动调用ArkTS存在问题
335浏览 • 1回复 待解决
A是Component, B是Dialog, C是Component
246浏览 • 1回复 待解决