HarmonyOS 二维数据数据传递最里面的对象,改变最里面的值页面不更新

HarmonyOS
2024-12-23 15:21:29
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
aquaa

示例参考如下:

@Observed
class ZLKShopCartModel {
  productId: string = ''
  productName: string = ''

  constructor(productId: string, productName: string) {
    this.productId = productId;
    this.productName = productName;
  }
}

@Observed
class ZLKShopCartListModel {
  showId: number = 0
  showList: Array<ZLKShopCartModel> = []

  constructor(showId: number, showList: Array<ZLKShopCartModel>) {
    this.showId = showId;

    this.showList = showList;
  }
}


@Component
struct ZLKShopCartItemView {
  @ObjectLink model: ZLKShopCartModel;

  build() {
    Column() {
      Text(this.model.productName)
      Button('修改为11111')
        .onClick(() => {
          this.model.productName = "11111"
        })
    }
  }
}

@Entry
@Component
struct ZLKShopCart {
  @State ZLKTShowList: Array<ZLKShopCartListModel> = [];

  onPageShow(): void {
    this.ZLKTShowList =
      [new ZLKShopCartListModel(1, [new ZLKShopCartModel("02233", 'ArkUI'), new ZLKShopCartModel("02233", 'ArkTS')])]
  }

  build() {
    Column() {
      List() {
        ForEach(this.ZLKTShowList, (listModel: ZLKShopCartListModel, index: number) => {
          ListItemGroup() {
            ForEach(listModel.showList, (model: ZLKShopCartModel, row: number) => {
              ZLKShopCartItemView({
                model: model
              })
            })
          }
        })
      }
    }
  }
}

关于此类场景也可以参考:

https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/quick-start/arkts-observed-and-objectlink.md#%E5%AF%B9%E8%B1%A1%E6%95%B0%E7%BB%84

分享
微博
QQ
微信
回复
2024-12-23 18:24:57
相关问题
如何查看mysql表里面的数据
2724浏览 • 1回复 待解决
HarmonyOS 数据传递问题
778浏览 • 1回复 待解决
HarmonyOS zxing二维
416浏览 • 1回复 待解决
HarmonyOS 二维码生成失败
227浏览 • 1回复 待解决
HarmonyOS 二维数组刷新问题
688浏览 • 1回复 待解决
HarmonyOS worker间数据传递
342浏览 • 1回复 待解决
HarmonyOS 二维码条码扫描识别
851浏览 • 1回复 待解决
ArkTS里面的?. 是什么意思
2542浏览 • 1回复 待解决