HarmonyOS 对象内数组内对象属性的修改问题

如demo的层级关系,点击后向修改原对象内一个数组内对象的某个属性失败。

@Observed
export class ChannelColumn {
  /**
   * 对象唯一标识
   */
  objectKey: string = ""
  videoInfo_list: ObservedArray<TestInfo> = new ObservedArray()
}

@Observed
export class ObservedArray<T> extends Array<T> {
  constructor(args?: T[]) {
    if (args instanceof Array) {
      super(...args);
    } else {
      super();
    }
  }
}

@Observed
export class TestInfo {
  isFoldFor1003: boolean = true;
  //vip频道改版
  maintitle: string = '';

  constructor(isFoldFor1003: boolean, maintitle: string) {
    this.isFoldFor1003 = this.isFoldFor1003;
    this.maintitle = maintitle
  }
}

@Component
export struct AEnterTest {
  @State @Watch('columnItemChange') columnItem: ChannelColumn = new ChannelColumn()

  aboutToAppear(): void {
    let videoInfo_list: ObservedArray<TestInfo> = new ObservedArray()
    videoInfo_list.push(new TestInfo(false, '测试数据1'))
    videoInfo_list.push(new TestInfo(false, '测试数据2'))
    videoInfo_list.push(new TestInfo(false, '测试数据3'))
    this.columnItem.videoInfo_list = videoInfo_list
  }

  columnItemChange() {
    console.log("columnItemChange")
    for (let i = 0; i < 3; i++) {
      console.debug('修改结果=' + this.columnItem.videoInfo_list[i].maintitle + ':' +
      this.columnItem.videoInfo_list[i].isFoldFor1003)
    }
  }

  build() {
    Row() {
      ChannelBaseTemplateView({
        columnItem: this.columnItem,
      })
    }
  }
}

@Component
export struct ChannelBaseTemplateView {
  @ObjectLink columnItem: ChannelColumn

  build() {
    SpecialPicText({ columnItem: this.columnItem })
  }
}

@Component
export struct SpecialPicText {
  @ObjectLink columnItem: ChannelColumn

  build() {
    Column() {
      Text('测试点击刷新数组内的对象的属性问题')
        .id('test')
        .width('100%')
        .height(50)
        .align(Alignment.Center)
        .onClick(() => {
          this.columnItem.videoInfo_list[0].isFoldFor1003 = false;
        }
        )
    }
  }
}
HarmonyOS
5天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280

只修改list中的元素属性无法被监听到,需要对整体的list元素了调整了才能监听到。或者只有@ObservedV2和@Trace修饰需要监听的类对象的属性,可以实现动态的UI刷新。

@ObservedV2文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-new-observedv2-and-trace-V5

代码调整如下:

@Observed
export class ChannelColumn {
  /**
   * 对象唯一标识
   */
  objectKey: string = ""
  videoInfo_list: ObservedArray<TestInfo> = new ObservedArray()
}

@Observed
export class ObservedArray<T> extends Array<T> {
  constructor(args?: T[]) {
    if (args instanceof Array) {
      super(...args);
    } else {
      super();
    }
  }
}

//方式一:@Observed
//方式二:@ObservedV2
export class TestInfo {
  //方式一: isFoldFor1003: boolean = true;
  //方式二: @Trace isFoldFor1003: boolean = true;
  //vip频道改版
  maintitle: string = '';

  constructor(isFoldFor1003: boolean, maintitle: string) {
    this.isFoldFor1003 = isFoldFor1003;
    this.maintitle = maintitle
  }
}

@Entry
@Component
export struct AEnterTest11 {
  @State columnItem: ChannelColumn = new ChannelColumn()

  aboutToAppear(): void {
    let videoInfo_list: ObservedArray<TestInfo> = new ObservedArray()
    videoInfo_list.push(new TestInfo(false, '测试数据1'))
    videoInfo_list.push(new TestInfo(false, '测试数据2'))
    videoInfo_list.push(new TestInfo(false, '测试数据3'))
    this.columnItem.videoInfo_list = videoInfo_list
  }

  build() {
    Column() {
      ForEach(this.columnItem.videoInfo_list, (item: TestInfo, index) => {
        Text(item.maintitle + item.isFoldFor1003)
      })
      Row() {
        ChannelBaseTemplateView11({
          columnItem: this.columnItem,
        })
      }
    }
  }
}

@Component
export struct ChannelBaseTemplateView11 {
  @ObjectLink columnItem: ChannelColumn

  build() {
    SpecialPicText11({ columnItem: this.columnItem })
  }
}

@Component
export struct SpecialPicText11 {
  @ObjectLink columnItem: ChannelColumn

  build() {
    Column() {
      Text('测试点击刷新数组内的对象的属性问题')
        .id('test')
        .width('100%')
        .height(50)
        .align(Alignment.Center)
        .onClick(() => {
          console.info("测试点击刷新数组内的对象的属性")
          // 只修改list中的元素属性无法被监听到
          this.columnItem.videoInfo_list[0].isFoldFor1003 = !this.columnItem.videoInfo_list[0].isFoldFor1003;
          // list变动了,才会被监听生效  //方式一加上下两行代码可以实现监听
          this.columnItem.videoInfo_list.push(new TestInfo(false, '测试数据99'))
          this.columnItem.videoInfo_list.pop()
        }
        )
    }
  }
}
分享
微博
QQ
微信
回复
5天前
相关问题
如何监听数组对象属性变化
2562浏览 • 1回复 待解决
HarmonyOS 对象数组排序问题
199浏览 • 1回复 待解决
HarmonyOS endable对象属性napi无法修改
58浏览 • 1回复 待解决
HarmonyOS 是否支持对象属性进行修改
56浏览 • 1回复 待解决
HarmonyOS 数组对象排序
211浏览 • 1回复 待解决
为什么FA模型不支持进程对象共享
2216浏览 • 1回复 待解决
HarmonyOS 数组对象数据刷新
197浏览 • 1回复 待解决
HarmonyOS @State可以修饰对象数组
127浏览 • 1回复 待解决
HarmonyOS 如何修改H5localstorage
45浏览 • 1回复 待解决
HarmonyOS 如何遍历对象属性
113浏览 • 1回复 待解决
HarmonyOS @state可以修饰对象数组
155浏览 • 1回复 待解决