关于harmonyos:HarmonyOSObserved装饰器和ObjectLink嵌套类对象属性变化

4次阅读

共计 6558 个字符,预计需要花费 17 分钟才能阅读完成。

@Observed 装璜器和 @ObjectLink 装璜器:嵌套类对象属性变动

概述

@ObjectLink 和 @Observed 类装璜器用于在波及嵌套对象或数组的场景中进行双向数据同步:

  • 被 @Observed 装璜的类,能够被察看到属性的变动;
  • 子组件中 @ObjectLink 装璜器装璜的状态变量用于接管 @Observed 装璜的类的实例,和父组件中对应的状态变量建设双向数据绑定。这个实例能够是数组中的被 @Observed 装璜的项,或者是 class
    object 中的属性,这个属性同样也须要被 @Observed 装璜。
  • 独自应用 @Observed 是没有任何作用的,须要搭配 @ObjectLink 或者 @Prop 应用。

限度条件

  • 应用 @Observed 装璜 class 会扭转 class 原始的原型链,@Observed 和其余类装璜器装璜同一个 class 可能会带来问题。
  • @ObjectLink 装璜器不能在 @Entry 装璜的自定义组件中应用。

装璜器阐明

@ObjectLink 装璜的数据为可读示例。

// 容许 @ObjectLink 装璜的数据属性赋值
this.objLink.a= ...
// 不容许 @ObjectLink 装璜的数据本身赋值
this.objLink= ...

阐明

@ObjectLink 装璜的变量不能被赋值,如果要应用赋值操作,请应用 @Prop。

  • @Prop 装璜的变量和数据源的关系是是单向同步,@Prop 装璜的变量在本地拷贝了数据源,所以它容许本地更改,如果父组件中的数据源有更新,@Prop 装璜的变量本地的批改将被笼罩;
  • @ObjectLink 装璜的变量和数据源的关系是双向同步,@ObjectLink 装璜的变量相当于指向数据源的指针。如果一旦产生 @ObjectLink 装璜的变量的赋值,则同步链将被打断。

变量的传递 / 拜访规定阐明

图 1 初始化规定图示

察看变动和行为表现

察看的变动

@Observed 装璜的类,如果其属性为非简略类型,比方 class、Object 或者数组,也须要被 @Observed 装璜,否则将察看不到其属性的变动。

class ClassA {
  public c: number;
  constructor(c: number) {this.c = c;}
}
@Observed
class ClassB {
  public a: ClassA;
  public b: number;
  constructor(a: ClassA, b: number) {
    this.a = a;
    this.b = b;
  }
}

以上示例中,ClassB 被 @Observed 装璜,其成员变量的赋值的变动是能够被察看到的,但对于 ClassA,没有被 @Observed 装璜,其属性的批改不能被察看到。

@ObjectLink b: ClassB
// 赋值变动能够被察看到
this.b.a = new ClassA(5)
this.b.b = 5
// ClassA 没有被 @Observed 装璜,其属性的变动察看不到
this.b.a.c = 5

@ObjectLink:@ObjectLink 只能接管被 @Observed 装璜 class 的实例,能够察看到:

  • 其属性的数值的变动,其中属性是指 Object.keys(observedObject)返回的所有属性,示例请参考嵌套对象。
  • 如果数据源是数组,则能够察看到数组 item 的替换,如果数据源是 class,可察看到 class 的属性的变动,示例请参考对象数组。

框架行为

1. 初始渲染:

a.@Observed 装璜的 class 的实例会被不通明的代理对象包装,代理了 class 上的属性的 setter 和 getter 办法

b. 子组件中 @ObjectLink 装璜的从父组件初始化,接管被 @Observed 装璜的 class 的实例,@ObjectLink 的包装类会将本人注册给 @Observed class。

2. 属性更新:当 @Observed 装璜的 class 属性扭转时,会走到代理的 setter 和 getter,而后遍历依赖它的 @ObjectLink 包装类,告诉数据更新。

应用场景

嵌套对象

以下是嵌套类对象的数据结构。

// objectLinkNestedObjects.ets
let NextID: number = 1;
@Observed
class ClassA {
  public id: number;
  public c: number;
  constructor(c: number) {
    this.id = NextID++;
    this.c = c;
  }
}
@Observed
class ClassB {
  public a: ClassA;
  constructor(a: ClassA) {this.a = a;}
}

以下组件层次结构出现的是嵌套类对象的数据结构。

@Component
struct ViewA {
  label: string = 'ViewA1';
  @ObjectLink a: ClassA;
  build() {Row() {Button(`ViewA [${this.label}] this.a.c=${this.a.c} +1`)
        .onClick(() => {this.a.c += 1;})
    }
  }
}
@Entry
@Component
struct ViewB {@State b: ClassB = new ClassB(new ClassA(0));
  build() {Column() {
      // in low version,DevEco may throw a warning,but it does not matter.
      // you can still compile and run.
      ViewA({label: 'ViewA #1', a: this.b.a})
      ViewA({label: 'ViewA #2', a: this.b.a})
      Button(`ViewB: this.b.a.c+= 1`)
        .onClick(() => {this.b.a.c += 1;})
      Button(`ViewB: this.b.a = new ClassA(0)`)
        .onClick(() => {this.b.a = new ClassA(0);
        })
      Button(`ViewB: this.b = new ClassB(ClassA(0))`)
        .onClick(() => {this.b = new ClassB(new ClassA(0));
        })
    }
  }
}

ViewB 中的事件句柄:

  • this.b.a = new ClassA(0) 和 this.b = new ClassB(new ClassA(0)):
    对 @State 装璜的变量 b 和其属性的批改。
  • this.b.a.c = …
    :该变动属于第二层的变动,@State 无奈察看到第二层的变动,然而 ClassA 被 @Observed 装璜,ClassA 的属性 c 的变动能够被 @ObjectLink 察看到。

ViewA 中的事件句柄:

  • this.a.c += 1:对 @ObjectLink 变量 a 的批改,将触发 Button 组件的刷新。@ObjectLink 和 @Prop 不同,@ObjectLink 不拷贝来自父组件的数据源,而是在本地构建了指向其数据源的援用。
  • @ObjectLink 变量是只读的,this.a = new
    ClassA(…)是不容许的,因为一旦赋值操作产生,指向数据源的援用将被重置,同步将被打断。

对象数组

对象数组是一种罕用的数据结构。以下示例展现了数组对象的用法。

@Component
struct ViewA {
  // 子组件 ViewA 的 @ObjectLink 的类型是 ClassA
  @ObjectLink a: ClassA;
  label: string = 'ViewA1';
  build() {Row() {Button(`ViewA [${this.label}] this.a.c = ${this.a.c} +1`)
        .onClick(() => {this.a.c += 1;})
    }
  }
}
@Entry
@Component
struct ViewB {// ViewB 中有 @State 装璜的 ClassA[]
  @State arrA: ClassA[] = [new ClassA(0), new ClassA(0)];
  build() {Column() {
      ForEach(this.arrA,
        (item) => {ViewA({ label: `#${item.id}`, a: item })
        },
        (item) => item.id.toString())
      // 应用 @State 装璜的数组的数组项初始化 @ObjectLink,其中数组项是被 @Observed 装璜的 ClassA 的实例
      ViewA({label: `ViewA this.arrA[first]`, a: this.arrA[0] })
      ViewA({label: `ViewA this.arrA[last]`, a: this.arrA[this.arrA.length-1] })

      Button(`ViewB: reset array`)
        .onClick(() => {this.arrA = [new ClassA(0), new ClassA(0)];
        })
      Button(`ViewB: push`)
        .onClick(() => {this.arrA.push(new ClassA(0))
        })
      Button(`ViewB: shift`)
        .onClick(() => {this.arrA.shift()
        })
      Button(`ViewB: chg item property in middle`)
        .onClick(() => {this.arrA[Math.floor(this.arrA.length / 2)].c = 10;
        })
      Button(`ViewB: chg item property in middle`)
        .onClick(() => {this.arrA[Math.floor(this.arrA.length / 2)] = new ClassA(11);
        })
    }
  }
}
  • this.arrA[Math.floor(this.arrA.length/2)] = new ClassA(..)
    :该状态变量的扭转触发 2 次更新:

    a.ForEach:数组项的赋值导致 ForEach 的 itemGenerator 被批改,因而数组项被辨认为有更改,ForEach 的 item builder 将执行,创立新的 ViewA 组件实例。

    b.ViewA({label: ViewA this.arrA[last], a: this.arrA[this.arrA.length-1] }):上述更改扭转了数组中第二个元素,所以绑定 this.arrA[1]的 ViewA 将被更新;

  • this.arrA.push(new ClassA(0)):将触发 2 次不同成果的更新:

    a.ForEach:新增加的 ClassA 对象对于 ForEach 是未知的 itemGenerator,ForEach 的 item builder 将执行,创立新的 ViewA 组件实例。

    b.ViewA({label: ViewA this.arrA[last], a: this.arrA[this.arrA.length-1] }):数组的最初一项有更改,因而引起第二个 ViewA 的实例的更改。对于 ViewA({label: ViewA this.arrA[first], a: this.arrA[0] }),数组的更改并没有触发一个数组项更改的扭转,所以第一个 ViewA 不会刷新。

  • this.arrA[Math.floor(this.arrA.length/2)].c:@State 无奈察看到第二层的变动,然而 ClassA 被 @Observed 装璜,ClassA 的属性的变动将被 @ObjectLink 察看到。

二维数组

应用 @Observed 察看二维数组的变动。能够申明一个被 @Observed 装璜的继承 Array 的子类。

@Observed
class StringArray extends Array<String> {}

应用 new StringArray()来结构 StringArray 的实例,new 运算符使得 @Observed 失效,@Observed 察看到 StringArray 的属性变动。

申明一个从 Array 扩大的类 class StringArray extends Array<String> {},并创立 StringArray 的实例。@Observed 装璜的类须要应用 new 运算符来构建 class 实例。

@Observed
class StringArray extends Array<String> {
}
@Component
struct ItemPage {
  @ObjectLink itemArr: StringArray;
  build() {Row() {Text('ItemPage')
        .width(100).height(100)

      ForEach(this.itemArr,
        item => {Text(item)
            .width(100).height(100)
        },
        item => item
      )
    }
  }
}
@Entry
@Component
struct IndexPage {@State arr: Array<StringArray> = [new StringArray(), new StringArray(), new StringArray()];
  build() {Column() {ItemPage({ itemArr: this.arr[0] })
      ItemPage({itemArr: this.arr[1] })
      ItemPage({itemArr: this.arr[2] })
      Divider()
      ForEach(this.arr,
        itemArr => {ItemPage({ itemArr: itemArr})
        },
        itemArr => itemArr[0]
      )
      Divider()
      Button('update')
        .onClick(() => {console.error('Update all items in arr');
          if (this.arr[0][0] !== undefined) {
            // 失常状况下须要有一个实在的 ID 来与 ForEach 一起应用,但此处没有
            // 因而须要确保推送的字符串是惟一的。this.arr[0].push(`${this.arr[0].slice(-1).pop()}${this.arr[0].slice(-1).pop()}`);
            this.arr[1].push(`${this.arr[1].slice(-1).pop()}${this.arr[1].slice(-1).pop()}`);
            this.arr[2].push(`${this.arr[2].slice(-1).pop()}${this.arr[2].slice(-1).pop()}`);
          } else {this.arr[0].push('Hello');
            this.arr[1].push('World');
            this.arr[2].push('!');
          }
        })
    }
  }
}

作为一名合格一线开发程序员,大家心里必定会有很多疑难!鸿蒙零碎这么弱小~~

为了可能让大家跟上互联网时代的技术迭代,在这里跟大家分享一下我本人近期学习心得以及参考网上材料整顿出的一份最新版的鸿蒙学习晋升材料 ,有须要的小伙伴自行支付, 限时开源,先到先得~~~~

支付以下高清学习路线原图请点击→《鸿蒙全套学习指南》纯血鸿蒙 HarmonyOS 根底技能学习路线图

支付以上残缺高清学习路线图,请点击→《鸿蒙开发学习之利用模型》小编本人整顿的局部学习材料(蕴含有高清视频、开发文档、电子书籍等)

以上分享的学习路线都适宜哪些人跟着学习?

  • - 应届生 / 计算机专业通过学习鸿蒙新兴技术,入行互联网,将来高起点待业。
  • - 0 根底转行提前布局新方向,抓住风口,自我晋升,取得更多就业机会。
  • - 技术晋升 / 进阶跳槽倒退瓶颈期,晋升职场竞争力,疾速把握鸿蒙技术,享受蓝海红利。

最初

鸿蒙开发学习是一个系统化的过程,从基础知识的学习到实战技能的锻炼 ,再到对前沿技术的摸索,每一环节都至关重要。 心愿这份教程材料能帮忙您疾速入门并在鸿蒙开发之路上步步攀升,成就一番事业。让咱们一起乘风破浪,拥抱鸿蒙生态的广大将来!

如果你感觉这篇内容对你有帮忙,我想麻烦大家动动小手给我:点赞,转发,有你们的『点赞和评论』,才是我发明的能源。

关注我,同时能够期待后续文章 ing,不定期分享原创常识。

想要获取更多残缺鸿蒙最新 VIP 学习材料,请点击→《鸿蒙根底入门学习指南》

正文完
 0