HarmonyOS @Observed和@ObservedLink使用问题

1.在PageA定义了一个 @State dataList: Student[] = [],Student用@Observed修饰。

2.网络请求后,给dataList赋值

3.使用dataList创建List组件,ListItem中的子组件为ComponentB

4.ComponentB中定义一个@ObjectLink stu: Student,此时点击改变stu.name之后,组件未刷新

请问是什么原因导致的?

HarmonyOS
2024-12-26 13:43:29
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zbw_apple

可以参考下面这个demo样例

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, "app1"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 2, "app2"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 3, "app3"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.app_icon"), 4, "app4"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.startIcon"), 5, "app5"));
    this.mListData.push(new ItemInfo('游戏', $r("app.media.startIcon"), 6, "app6"));
  }

  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;
  // 列表数据的单个item对象数据,需要使用ObjectLink修饰监听,用于将数据变化传递给外部父组件的mListData
  @ObjectLink item: ItemInfo

  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 = "app23333"
      }else{
        this.item.childInfo.index = this.index;
        this.item.childInfo.grandsonInfo.content = "app" + 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.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.

判断应为name属性没有被监听到,另外可参考下@Observed 嵌套类属性变化常见问题:

https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-observed-and-objectlink-V5#常见问题

分享
微博
QQ
微信
回复
2024-12-26 17:04:40
相关问题
HarmonyOS @Observed不生效问题
1574浏览 • 1回复 待解决
HarmonyOS @Observed装饰器问题咨询
903浏览 • 1回复 待解决
HarmonyOS 使用Observed界面仍然不刷新
699浏览 • 1回复 待解决
HarmonyOS TabsSwiper联合使用问题
576浏览 • 1回复 待解决
HarmonyOS 新状态管理框架@observed、@track
1484浏览 • 1回复 待解决
HarmonyOS 使用GridGriItem组合的问题
939浏览 • 1回复 待解决
HarmonyOS Web组件List的嵌套使用问题
1304浏览 • 1回复 待解决
requestrequestInStream的使用边界问题
3868浏览 • 1回复 待解决
关于Grid容器WaterFlow使用上的问题
1441浏览 • 1回复 待解决