上文所属的装饰器仅能观察到第一层的变化,但是在实际应用开发中,应用会根据开发需要,封装自己的 数据模型。对于多层嵌套的情况,比如二维数组,或者数组项class,或者class的属性是class,他们的第二层的属性变化是无法观察到的。这就引出了@Observed/@ObjectLink装饰器
ObjectLink和@Observed类装饰器用于在设计嵌套对象或数组的场景中进行双向数据同步:
使用@Observed装饰的class会改变class原始的原型链,@Observed和其他类装饰器装饰同一个class可能会带来问题。
@Observed类装饰器 | 说明 |
---|---|
装饰器参数 | 无 |
类装饰器 | 装饰class。需要放在class的定义前,使用new创建类对象 |
@ObjectLink变量装饰器 | 说明 |
---|---|
装饰器参数 | 无 |
同步类型 | 不与父组件中的任何类型同步变量 |
允许装饰的变量类型 | 必须为被@Observed装饰的class实例,必须指定类型。不支持简单类型,可以使用@Prop@ObjectLink的属性是可以改变的,但是变量的分配是不允许的,也就是说这个装饰器装饰变量是只读的,不能被改变。 |
被装饰变量的初始值 | 不允许 |
@ObjectLink装饰的数据为可读示例。
//允许@ObjectLink装饰的数据属性赋值
this.objLink.a=...
//不允许@ObjectLink装饰的数据自身赋值
this.objLink= ...
@ObjectLink传递/访问 | 说明 |
---|---|
从父组件初始化 | 必须指定。初始化@ObjectLink装饰的变量必须同时满足以下场景:- 类型必须是@Observed装饰的class。- 初始化的数值需要是数组项,或者class的属性- 同步源的class或者数组必须是@State,@Link,@Provide,@Consume或者@ObjectLink装饰的数据。同步源是数组项的示例请参考对象数组。初始化的class的示例请参考嵌套对象。 |
与源对象同步 | 双向。 |
可以初始化子组件 | 允许,可用于初始化常规变量,@State,@Link,@Prop,@Provide |
@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的实例,可以观察到:
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(){
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中的事件句柄:
ViewA中的事件句柄:
对象数组是一种常用的数据结构。以下示例展示了数组对象的用法。
@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.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);
})
}
}
}
ViewA this.arrA[first]
,a:this.arrA[0]}):上述更改改变了数组中第一个元素,所以绑定this.arrA[0] 的ViewA组将被更新;ViewA this.arrA[last]
,a:this.arrA[this.arrA.length-1]}):数组的最后一项有更改,因此引起第二个ViewA的实例的更改。对于ViewA({label: ViewA this.arrA[first]
,a:this.arrA[0]},数组的更改并没有触发一个数组项更改的改变,所以第一个ViewA不会刷新。
使用@Observed观察二维数组的变化。可以声明一个呗@Observed装饰的集成Array的子类
@Observed
class StringArray extends Array<String>{
}
使用new StringArray()来构造StringArray的实例,new运算符使得@Observed生效,@Observed观察到StringArray的属性变化。
声明一个从Array扩展的类class StringArray extends Array{},并创建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
@Componnet
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('!');
}
})
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有