HarmonyOS 嵌套数组使用observed修饰后,修改数组值不更新

HarmonyOS
2024-12-25 14:08:26
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280

@Observed配合@ObjectLink是可以监听复杂对象深层属性变化。嵌套对象的监听需要注意几点:

1、@ObjectLink的属性是可以改变的,但是变量的分配是不允许的。即被监听对象不能重新被赋值,只能修改对象的属性的值。

2、监听嵌套的多层复杂对象时,每层对象的类都需要@Observed。

3、对数组、集合等一些特殊对象的监听指南中也有特别说明。

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5

参考示例如下:

import { util } from '@kit.ArkTS';

/** * 三级数据结构 */
@Observed
  // 每一级数据结构都需要用Observed修饰
class GrandsonInfo {
  content: string = "";
}

/** * 二级数据结构 */
@Observed
  // 每一级数据结构都需要用Observed修饰
class ChildInfo {
  index: number;
  grandsonInfo: GrandsonInfo;

  constructor(index: number, content: string) {
    this.index = index;
    this.grandsonInfo = new GrandsonInfo();
    this.grandsonInfo.content = content;
  }
}

/** * 一级数据结构 */
@Observed
  // 每一级数据结构都需要用Observed修饰
class ItemInfo {
  key: string = util.generateRandomUUID(true);
  name: string;
  icon: Resource;
  childInfo: ChildInfo;
  select: boolean;

  constructor(name: string, icon: Resource, index: number, content: string) {
    this.name = name;
    this.icon = icon;
    this.childInfo = new ChildInfo(index, content);
    this.select = false;
  }
}

/** * 多层嵌套刷新渲染 */
@Entry
@Component
struct ObservedPage {
  private TAG: string = "ObservedPage";
  @State mListData: Array<ItemInfo> = [];

  aboutToAppear(): void {
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 1, "文本1"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 2, "文本2"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 3, "文本3"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 4, "文本4"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.startIcon"), 5, "文本5"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.startIcon"), 6, "文本6"));
  }

  build() {
    List() {
      ForEach(this.mListData, (item: ItemInfo, index: number) => {
        ListItem() {
          ItemView({ item: item, index: index })
        }
      }, (item: ItemInfo) => JSON.stringify(item))
    }.width("100%").height("100%").padding({ left: px2vp(60), right: px2vp(60) })
  }
}

@Component
struct ItemView {
  private TAG: string = "ItemView";
  @Prop index: number = 0;
  @ObjectLink item: ItemInfo // 列表数据的单个item对象数据,需要使用ObjectLink修饰监听,用于将数据变化传递给外部父组件的mListData

  build() {
    Row() {
      Image(this.item.icon).width(px2vp(200)).height(px2vp(200))
      Text(this.item.name + "(" + this.item.childInfo.index + ")" + " [ " + this.item.childInfo.grandsonInfo.content +
        " ] ").fontSize(px2fp(52))
      Blank()
      if (this.isLog(this.item, this.index)) {
        if (this.item.select) {
          Image($r("app.media.app_icon")).size({ width: px2vp(72), height: px2vp(72) })
        }
      }
    }.width('100%').justifyContent(FlexAlign.Start).onClick(() => {
      this.item.select = !this.item.select;
      if (this.item.select) {
        this.item.childInfo.index = 666;
        this.item.childInfo.grandsonInfo.content = "文本23333"
      } else {
        this.item.childInfo.index = this.index;
        this.item.childInfo.grandsonInfo.content = "文本" + this.index;
      }
      console.log(this.TAG, " ItemView onClick: " + this.index + " item.select: " + this.item.select);
    })
  }

  private isLog(item: ItemInfo, index: number) {
    console.log(this.TAG, " ItemView isLog index: " + index + " item.select: " + item.select);
    return true;
  }
}
  • 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.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
分享
微博
QQ
微信
回复
2024-12-25 17:09:47
相关问题
数组嵌套数组场景的懒加载实现
1100浏览 • 1回复 待解决
HarmonyOS 嵌套数组元素的UI刷新方案
1014浏览 • 1回复 待解决
readonly修饰数组无法获取数组元素
3084浏览 • 1回复 待解决
HarmonyOS @State可以修饰对象数组
680浏览 • 1回复 待解决
HarmonyOS @state可以修饰对象数组
649浏览 • 1回复 待解决