wordpress做中英文站,免费安全,什么叫网站维护,临淄信息港招聘Observed装饰器和ObjectLink装饰器#xff1a;嵌套类对象属性变化
上文所述的装饰器仅能观察到第一层的变化#xff0c;但是在实际应用开发中#xff0c;应用会根据开发需要#xff0c;封装自己的数据模型。对于多层嵌套的情况#xff0c;比如二维数组#xff0c;或者数…Observed装饰器和ObjectLink装饰器嵌套类对象属性变化
上文所述的装饰器仅能观察到第一层的变化但是在实际应用开发中应用会根据开发需要封装自己的数据模型。对于多层嵌套的情况比如二维数组或者数组项class或者class的属性是class他们的第二层的属性变化是无法观察到的。这就引出了Observed/ObjectLink装饰器。 概述 ObjectLink和Observed类装饰器用于在涉及嵌套对象或数组的场景中进行双向数据同步
被Observed装饰的类可以被观察到属性的变化子组件中ObjectLink装饰器装饰的状态变量用于接收Observed装饰的类的实例和父组件中对应的状态变量建立双向数据绑定。这个实例可以是数组中的被Observed装饰的项或者是class object中是属性这个属性同样也需要被Observed装饰。单独使用Observed是没有任何作用的需要搭配ObjectLink或者Prop使用。
限制条件 使用Observed装饰class会改变class原始的原型链Observed和其他类装饰器装饰同一个class可能会带来问题。
装饰器说明 Observed类装饰器 说明 装饰器参数 无 类装饰器 装饰class。需要放在class的定义前使用new创建类对象。 ObjectLink变量装饰器 说明 装饰器参数 无 同步类型 不与父组件中的任何类型同步变量。 允许装饰的变量类型 必须为被Observed装饰的class实例必须指定类型。 不支持简单类型可以使用Prop。 ObjectLink的属性是可以改变的但是变量的分配是不允许的也就是说这个装饰器装饰变量是只读的不能被改变。 被装饰变量的初始值 不允许。
ObjectLink装饰的数据为可读示例。
// 允许ObjectLink装饰的数据属性赋值
this.objLink.a ...
// 不允许ObjectLink装饰的数据自身赋值
this.objLink ... 说明 ObjectLink装饰的变量不能被赋值如果要使用赋值操作请使用Prop。 Prop装饰的变量和数据源的关系是是单向同步Prop装饰的变量在本地拷贝了数据源所以它允许本地更改如果父组件中的数据源有更新Prop装饰的变量本地的修改将被覆盖ObjectLink装饰的变量和数据源的关系是双向同步ObjectLink装饰的变量相当于指向数据源的指针。如果一旦发生ObjectLink装饰的变量的赋值则同步链将被打断。 变量的传递/访问规则说明 ObjectLink传递/访问 说明 从父组件初始化 必须指定。 初始化ObjectLink装饰的变量必须同时满足以下场景 类型必须是Observed装饰的class。初始化的数值需要是数组项或者class的属性。同步源的class或者数组必须是StateLinkProvideConsume或者ObjectLink装饰的数据。 与源对象同步 双向。 可以初始化子组件 允许可用于初始化常规变量、State、Link、Prop、Provide
图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
ObjectLinkObjectLink只能接收被Observed装饰class的实例可以观察到
其属性的数值的变化其中属性是指Object.keys(observedObject)返回的所有属性。
如果数据源是数组则可以观察到数组item的替换如果数据源是class可观察到class的属性的变化。
框架行为 初始渲染 Observed装饰的class的实例会被不透明的代理对象包装代理了class上的属性的setter和getter方法子组件中ObjectLink装饰的从父组件初始化接收被Observed装饰的class的实例ObjectLink的包装类会将自己注册给Observed class。属性更新当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中的事件句柄
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的类型是ClassAObjectLink 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次更新 ForEach数组项的赋值导致ForEach的itemGenerator被修改因此数组项被识别为有更改ForEach的item builder将执行创建新的ViewA组件实例。ViewA({ label: ViewA this.arrA[first], a: this.arrA[0] })上述更改改变了数组中第一个元素所以绑定this.arrA[0]的ViewA将被更新this.arrA.push(new ClassA(0)) 将触发2次不同效果的更新 ForEach新添加的ClassA对象对于ForEach是未知的itemGeneratorForEach的item builder将执行创建新的ViewA组件实例。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)].cState无法观察到第二层的变化但是ClassA被Observed装饰ClassA的属性的变化将被ObjectLink观察到。
二维数组 使用Observed观察二维数组的变化。可以声明一个被Observed装饰的继承Array的子类。
Observed
class StringArray extends ArrayString {
}
使用new StringArray()来构造StringArray的实例new运算符使得Observed生效Observed观察到StringArray的属性变化。
声明一个从Array扩展的类class StringArray extends ArrayString {}并创建StringArray的实例。Observed装饰的类需要使用new运算符来构建class实例。
Observed
class StringArray extends ArrayString {
}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: ArrayStringArray [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(!);}})}}
} 文章转载自: http://www.morning.nffwl.cn.gov.cn.nffwl.cn http://www.morning.pkrb.cn.gov.cn.pkrb.cn http://www.morning.kaylyea.com.gov.cn.kaylyea.com http://www.morning.qzfjl.cn.gov.cn.qzfjl.cn http://www.morning.tqldj.cn.gov.cn.tqldj.cn http://www.morning.rlkgc.cn.gov.cn.rlkgc.cn http://www.morning.lfgql.cn.gov.cn.lfgql.cn http://www.morning.wtbzt.cn.gov.cn.wtbzt.cn http://www.morning.mzcrs.cn.gov.cn.mzcrs.cn http://www.morning.qgjxy.cn.gov.cn.qgjxy.cn http://www.morning.cryb.cn.gov.cn.cryb.cn http://www.morning.nqlcj.cn.gov.cn.nqlcj.cn http://www.morning.bgpb.cn.gov.cn.bgpb.cn http://www.morning.bpmnh.cn.gov.cn.bpmnh.cn http://www.morning.bnbtp.cn.gov.cn.bnbtp.cn http://www.morning.pzdxg.cn.gov.cn.pzdxg.cn http://www.morning.mnsmb.cn.gov.cn.mnsmb.cn http://www.morning.sypby.cn.gov.cn.sypby.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.rxgnn.cn.gov.cn.rxgnn.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.khyqt.cn.gov.cn.khyqt.cn http://www.morning.plydc.cn.gov.cn.plydc.cn http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.knswz.cn.gov.cn.knswz.cn http://www.morning.rzbcz.cn.gov.cn.rzbcz.cn http://www.morning.zrkp.cn.gov.cn.zrkp.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.zrbpx.cn.gov.cn.zrbpx.cn http://www.morning.qxkjy.cn.gov.cn.qxkjy.cn http://www.morning.xckrj.cn.gov.cn.xckrj.cn http://www.morning.feites.com.gov.cn.feites.com http://www.morning.dwwbt.cn.gov.cn.dwwbt.cn http://www.morning.wqgr.cn.gov.cn.wqgr.cn http://www.morning.yrwqz.cn.gov.cn.yrwqz.cn http://www.morning.poapal.com.gov.cn.poapal.com http://www.morning.fmtfj.cn.gov.cn.fmtfj.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.zgztn.cn.gov.cn.zgztn.cn http://www.morning.lrgfd.cn.gov.cn.lrgfd.cn http://www.morning.wqsjx.cn.gov.cn.wqsjx.cn http://www.morning.jyzxt.cn.gov.cn.jyzxt.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.bwttj.cn.gov.cn.bwttj.cn http://www.morning.ampingdu.com.gov.cn.ampingdu.com http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.xzqzd.cn.gov.cn.xzqzd.cn http://www.morning.wtcbl.cn.gov.cn.wtcbl.cn http://www.morning.easiuse.com.gov.cn.easiuse.com http://www.morning.ynwdk.cn.gov.cn.ynwdk.cn http://www.morning.owenzhi.com.gov.cn.owenzhi.com http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.bzfld.cn.gov.cn.bzfld.cn http://www.morning.mtgnd.cn.gov.cn.mtgnd.cn http://www.morning.fyxr.cn.gov.cn.fyxr.cn http://www.morning.wynnb.cn.gov.cn.wynnb.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.ycpnm.cn.gov.cn.ycpnm.cn http://www.morning.lzqnj.cn.gov.cn.lzqnj.cn http://www.morning.mhmdx.cn.gov.cn.mhmdx.cn http://www.morning.xrftt.cn.gov.cn.xrftt.cn http://www.morning.hlmkx.cn.gov.cn.hlmkx.cn http://www.morning.mjglk.cn.gov.cn.mjglk.cn http://www.morning.qmbgb.cn.gov.cn.qmbgb.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.qxwgx.cn.gov.cn.qxwgx.cn http://www.morning.xhddb.cn.gov.cn.xhddb.cn http://www.morning.qbrdg.cn.gov.cn.qbrdg.cn http://www.morning.jwefry.cn.gov.cn.jwefry.cn http://www.morning.nd-test.com.gov.cn.nd-test.com http://www.morning.mcmpq.cn.gov.cn.mcmpq.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.zqzzn.cn.gov.cn.zqzzn.cn http://www.morning.kpcxj.cn.gov.cn.kpcxj.cn http://www.morning.darwallet.cn.gov.cn.darwallet.cn http://www.morning.lsnnq.cn.gov.cn.lsnnq.cn http://www.morning.fppzc.cn.gov.cn.fppzc.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.jokesm.com.gov.cn.jokesm.com