HarmonyOS lazyForEach里面组件复用,如果组件里面是@ObjectLink修饰的某个状态变量该怎么重用

1,lazyForEach里面组件复用,如果组件里面是@ObjectLink修饰的某个状态变量(例如@ObjectLink item:XXXModel;),在组件重用时候重写aboutToReuse,在aboutToReuse里面this.item=params.item as XXXModel,运行就会crash,报item没有set方法,也就是@ObjectLink修饰的变量不能在组件内部赋值,这种情况下组件怎么复用

2、组件重用时候会触发aboutToReuse,那么还好触发aboutToAppear吗,我理解的是不会,但是我测试是触发了

HarmonyOS
2天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Excelsior_abit

示例参考如下:

@Reusable
@Component
struct Gizmos{
  private str: string = ''
  private reuseChain: string = ''
  @State textStr: string = `build ${this.str}`
  @ObjectLink person:Person

  aboutToAppear() {
    this.textStr = `build ${this.str}`
    this.reuseChain = `${this.str}`
  }

  aboutToReuse(param: ESObject) {
    let preStr: string = this.str;
    this.str = param.str
    this.reuseChain = `${this.reuseChain}->${this.str}`
    this.textStr = `reuse ${preStr} as ${this.str}, reuseChain ${this.reuseChain}`
  }

  build() {
    Column(){
      Text(this.textStr)
      Text(this.person.name)
    }
    .width('100%')
    .height(160)
    .backgroundColor(Color.Green)
  }
}

class BasicDataSource implements IDataSource {
  private listeners: DataChangeListener[] = [];

  public totalCount(): number {
    return 0;
  }

  public getData(index: number): string {
    return '';
  }

  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);
    })
  }

  notifyDataMove(from: number, to: number): void {
    this.listeners.forEach(listener => {
      listener.onDataMove(from, to);
    })
  }
}

class MyDataSource2 extends BasicDataSource {
  private dataArray: string[] = [];

  public totalCount(): number {
    return this.dataArray.length;
  }

  public getData(index: number): string {
    return this.dataArray[index];
  }

  public addData(index: number, data: string): void {
    this.dataArray.splice(index, 0, data);
    this.notifyDataAdd(index);
  }

  public pushData(data: string): void {
    this.dataArray.push(data);
    this.notifyDataAdd(this.dataArray.length - 1);
  }

  public deleteData(index: number): void {
    this.dataArray.splice(index, 1);
    this.notifyDataDelete(index);
  }
}

@Entry
@Component
struct ReusableDemo {

  data: MyDataSource2 = new MyDataSource2();

  @State flags:boolean[] = []
  @State persons:Person = new Person()

  aboutToAppear() {
    for (let i = 1; i < 21; i++) {
      this.data.pushData(`${i}`)
      if (i % 3 == 0) {
        this.flags.push(true)
      }
      else {
        this.flags.push(false)
      }
    }
  }

  build() {
    Grid() {
      LazyForEach(this.data, (item: string, index) => {
        GridItem() {
          Column() {
            Button('switch')
              .onClick(()=>{
                this.flags[index] = !this.flags[index];
                this.persons.name = '王林';
              })
            if(this.flags[index]) {
              Gizmos({str: this.data.getData(index),person:this.persons})
            }
          }
          .height(200)
        }
        .borderColor(Color.Black)
        .borderWidth(3)
      }, (item: string) => item)
    }
    .columnsTemplate('1fr 1fr')
  }
}

@Observed
class Person {
  name:string = '张三';
  age : number = 500;

  public onchang(){
    console.log(':::onchang');
  }
}
分享
微博
QQ
微信
回复
2天前
相关问题
HarmonyOS taskpool参数状态变量crash
43浏览 • 1回复 待解决
HarmonyOS constraintSize支持状态变量
235浏览 • 1回复 待解决
状态变量和常规变量有什么区别?
637浏览 • 2回复 待解决
HarmonyOS 状态变量不刷新问题
658浏览 • 1回复 待解决
ArkTS中如何监听状态变量变化?
1177浏览 • 1回复 待解决
关于状态变量@state必须知道
1387浏览 • 1回复 待解决