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
              })
            })
          }
        })
      }
    }
  }
}
  • 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.

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

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表里面的数据
3088浏览 • 1回复 待解决
HarmonyOS 数据传递问题
1213浏览 • 1回复 待解决
HarmonyOS 二维码生成失败
771浏览 • 1回复 待解决
HarmonyOS zxing二维
973浏览 • 1回复 待解决
HarmonyOS worker间数据传递
799浏览 • 1回复 待解决
ArkTS里面的?. 是什么意思
3165浏览 • 1回复 待解决
HarmonyOS 二维码条码扫描识别
1323浏览 • 1回复 待解决