HarmonyOS NEXT应用开发之多层嵌套类对象监听

介绍

本示例介绍使用@Observed装饰器和@ObjectLink装饰器来实现多层嵌套类对象属性变化的监听。

效果图预览

使用说明

  1. 加载完成后显示商品列表,点击刷新按钮可以刷新商品图片和价格。

实现思路

  1. 创建FistGoodsModel类,类对象是用@Observed修饰的类SecondGoodsItemList,SecondGoodsItemList类对象是用@Observed修饰的ThirdGoodsItem类,ThirdGoodsItem类对应的商品信息,是要被监听的对象。源码参考GoodsModel.ets
/**
 * 表示商品详细数据的类型,是嵌套类的第三层
 * @class
 */
@Observed
export class ThirdGoodsItem {
  imgSrc: Resource; // 商品图片
  price: string; // 商品价格

  constructor(imgSrc: Resource, price: string) {
    this.imgSrc = imgSrc;
    this.price = price;
  }
}

/**
 * 表示商品列表的类型,是嵌套类的第二层
 * @class
 */
@Observed
export class SecondGoodsItemList {
  itemList: Array<ThirdGoodsItem>;

  constructor(imgSrc: Array<ThirdGoodsItem>) {
    this.itemList = imgSrc;
  }
}

/**
 * 表示商品模型的类型,是嵌套类的首层
 * @class
 */
export class FistGoodsModel {
  itemList: SecondGoodsItemList;

  constructor(itemList: SecondGoodsItemList) {
    this.itemList = itemList;
  }
}
  1. 自定义组件中用@ObjectLink修饰对应class实例。源码参考ProductView.ets
@Component
export default struct GoodViewStruct {
  @Link model: FistGoodsModel;

  build() {
    Column() {
      SecondViews()
    }
  }
}

@Component
struct SecondViews {
  @ObjectLink data: SecondGoodsItemList

  build() {
    List() { ... }
  }
}

@Component
struct ThirdView {
  @ObjectLink item: ThirdGoodsItem

  build() {
    Column() { ... }
  }
}
  1. 更新第三层嵌套class ThirdGoodsItem的数据,UI刷新。源码参考VariableWatchView.ets
this.itemList.forEach((item, index) => {
  item.imgSrc = originItemList[index].imgSrc;
  item.price = originItemList[index].price;
}

高性能知识点

本示例介绍使用@Observed装饰器和@ObjectLink装饰器来解决需要单独监听多层嵌套类对象属性的方案。

工程结构&模块类型

VariableWatchView                               // har类型
|---model
|   |---GoodsModel.ets                          // 数据模型
|---view
|   |---ProductView.ets                         // 视图层-场景列表页面
|   |---VariableWatchView.ets                   // 视图层-场景主页面

模块依赖

不涉及

参考资料

@Observed装饰器和@ObjectLink装饰器:嵌套类对象属性变化

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:https://qr21.cn/FV7h05

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

大厂鸿蒙面试题:https://qr21.cn/FV7h05

鸿蒙开发面试大盘集篇(共计319页):https://qr21.cn/FV7h05

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

相关推荐

  1. Python scapy 构建嵌套数据包

    2024-03-17 03:54:04       26 阅读
  2. 对象\对象应用

    2024-03-17 03:54:04       20 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-17 03:54:04       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-17 03:54:04       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-17 03:54:04       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-17 03:54:04       18 阅读

热门阅读

  1. 突破编程_C++_查找算法(插值查找)

    2024-03-17 03:54:04       25 阅读
  2. 稳定币套利案例解析一 两个疑点

    2024-03-17 03:54:04       20 阅读
  3. 【Python学习笔记】Python近期总结

    2024-03-17 03:54:04       20 阅读
  4. 24计算机考研调剂 | 哈尔滨理工大学

    2024-03-17 03:54:04       22 阅读
  5. CentOS7下使用Dockers安装MinIO

    2024-03-17 03:54:04       16 阅读
  6. 【面经&八股】搜广推方向:面试记录(八)

    2024-03-17 03:54:04       19 阅读
  7. 程序员如何选择职业赛道

    2024-03-17 03:54:04       16 阅读
  8. LeetCode -- 76. 最小覆盖子串

    2024-03-17 03:54:04       18 阅读
  9. 前端如何识别上传的二维码---jsQR

    2024-03-17 03:54:04       18 阅读
  10. 计算机安全

    2024-03-17 03:54:04       17 阅读
  11. MySQL 中的锁机制详解

    2024-03-17 03:54:04       19 阅读
  12. transformer注意力权重系数绘图

    2024-03-17 03:54:04       18 阅读