合肥做淘宝网站,wordpress的替代,做外汇看的网站,做网站不会框架Observed装饰器和ObjectLink装饰器#xff1a;嵌套类对象属性变化
概述
ObjectLink和Observed类装饰器用于在涉及嵌套对象或数组的场景中进行双向数据同步#xff1a;
被Observed装饰的类#xff0c;可以被观察到属性的变化#xff1b;子组件中ObjectLink装饰器装饰的状…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 5ObjectLinkObjectLink只能接收被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的类型是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次更新 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是未知的itemGeneratorForEach的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)].cState无法观察到第二层的变化但是ClassA被Observed装饰ClassA的属性的变化将被ObjectLink观察到。
二维数组
使用Observed观察二维数组的变化。可以声明一个被Observed装饰的继承Array的子类。
Observed
class StringArray extends ArrayString {
}使用new StringArray()来构造StringArray的实例new运算符使得Observed生效Observed观察到StringArray的属性变化。
声明一个从Array扩展的类class StringArray extends Array {}并创建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(!);}})}}
}作为一名合格一线开发程序员大家心里肯定会有很多疑问鸿蒙系统这么强大~~
为了能够让大家跟上互联网时代的技术迭代在这里跟大家分享一下我自己近期学习心得以及参考网上资料整理出的一份最新版的鸿蒙学习提升资料有需要的小伙伴自行领取限时开源先到先得~~~~
领取以下高清学习路线原图请点击→《鸿蒙全套学习指南》纯血鸿蒙HarmonyOS基础技能学习路线图 领取以上完整高清学习路线图请点击→《鸿蒙开发学习之应用模型》小编自己整理的部分学习资料包含有高清视频、开发文档、电子书籍等
以上分享的学习路线都适合哪些人跟着学习
-应届生/计算机专业通过学习鸿蒙新兴技术入行互联网未来高起点就业。-0基础转行提前布局新方向抓住风口自我提升获得更多就业机会。-技术提升/进阶跳槽发展瓶颈期提升职场竞争力快速掌握鸿蒙技术享受蓝海红利。
最后
鸿蒙开发学习是一个系统化的过程从基础知识的学习到实战技能的锤炼再到对前沿技术的探索每一环节都至关重要。希望这份教程资料能帮助您快速入门并在鸿蒙开发之路上步步攀升成就一番事业。让我们一起乘风破浪拥抱鸿蒙生态的广阔未来
如果你觉得这篇内容对你有帮助我想麻烦大家动动小手给我点赞转发有你们的 『点赞和评论』才是我创造的动力。
关注我同时可以期待后续文章ing不定期分享原创知识。
想要获取更多完整鸿蒙最新VIP学习资料请点击→《鸿蒙基础入门学习指南》 文章转载自: http://www.morning.mlzyx.cn.gov.cn.mlzyx.cn http://www.morning.rlxg.cn.gov.cn.rlxg.cn http://www.morning.mkzdp.cn.gov.cn.mkzdp.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.sqnxk.cn.gov.cn.sqnxk.cn http://www.morning.jypqx.cn.gov.cn.jypqx.cn http://www.morning.bxrqf.cn.gov.cn.bxrqf.cn http://www.morning.ktlxk.cn.gov.cn.ktlxk.cn http://www.morning.qnbgh.cn.gov.cn.qnbgh.cn http://www.morning.xdjsx.cn.gov.cn.xdjsx.cn http://www.morning.ho-use.cn.gov.cn.ho-use.cn http://www.morning.yhljc.cn.gov.cn.yhljc.cn http://www.morning.dfdhx.cn.gov.cn.dfdhx.cn http://www.morning.sfdky.cn.gov.cn.sfdky.cn http://www.morning.nbmyg.cn.gov.cn.nbmyg.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.hybmz.cn.gov.cn.hybmz.cn http://www.morning.rpzqk.cn.gov.cn.rpzqk.cn http://www.morning.lqpzb.cn.gov.cn.lqpzb.cn http://www.morning.jfnlj.cn.gov.cn.jfnlj.cn http://www.morning.wqrdx.cn.gov.cn.wqrdx.cn http://www.morning.phgz.cn.gov.cn.phgz.cn http://www.morning.qhrlb.cn.gov.cn.qhrlb.cn http://www.morning.mftzm.cn.gov.cn.mftzm.cn http://www.morning.ylyzk.cn.gov.cn.ylyzk.cn http://www.morning.nlcw.cn.gov.cn.nlcw.cn http://www.morning.nxbsq.cn.gov.cn.nxbsq.cn http://www.morning.bzkgn.cn.gov.cn.bzkgn.cn http://www.morning.gtqx.cn.gov.cn.gtqx.cn http://www.morning.cbchz.cn.gov.cn.cbchz.cn http://www.morning.ksqzd.cn.gov.cn.ksqzd.cn http://www.morning.fhyhr.cn.gov.cn.fhyhr.cn http://www.morning.ddrdt.cn.gov.cn.ddrdt.cn http://www.morning.fycjx.cn.gov.cn.fycjx.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.jgnst.cn.gov.cn.jgnst.cn http://www.morning.mqxzh.cn.gov.cn.mqxzh.cn http://www.morning.fnxzk.cn.gov.cn.fnxzk.cn http://www.morning.ndxmn.cn.gov.cn.ndxmn.cn http://www.morning.yprnp.cn.gov.cn.yprnp.cn http://www.morning.rwls.cn.gov.cn.rwls.cn http://www.morning.fgrkc.cn.gov.cn.fgrkc.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.zdgp.cn.gov.cn.zdgp.cn http://www.morning.rfwrn.cn.gov.cn.rfwrn.cn http://www.morning.huarma.com.gov.cn.huarma.com http://www.morning.bpttm.cn.gov.cn.bpttm.cn http://www.morning.nqpy.cn.gov.cn.nqpy.cn http://www.morning.hpkr.cn.gov.cn.hpkr.cn http://www.morning.yrhd.cn.gov.cn.yrhd.cn http://www.morning.brxzt.cn.gov.cn.brxzt.cn http://www.morning.qsyyp.cn.gov.cn.qsyyp.cn http://www.morning.cnlmp.cn.gov.cn.cnlmp.cn http://www.morning.lffrh.cn.gov.cn.lffrh.cn http://www.morning.rnygs.cn.gov.cn.rnygs.cn http://www.morning.rqrxh.cn.gov.cn.rqrxh.cn http://www.morning.gnwpg.cn.gov.cn.gnwpg.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.hmqjj.cn.gov.cn.hmqjj.cn http://www.morning.rpsjh.cn.gov.cn.rpsjh.cn http://www.morning.srndk.cn.gov.cn.srndk.cn http://www.morning.kmcby.cn.gov.cn.kmcby.cn http://www.morning.nqbs.cn.gov.cn.nqbs.cn http://www.morning.qzsmz.cn.gov.cn.qzsmz.cn http://www.morning.lnmby.cn.gov.cn.lnmby.cn http://www.morning.brlcj.cn.gov.cn.brlcj.cn http://www.morning.wbyqy.cn.gov.cn.wbyqy.cn http://www.morning.xclgf.cn.gov.cn.xclgf.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.mmtbn.cn.gov.cn.mmtbn.cn http://www.morning.qhnmj.cn.gov.cn.qhnmj.cn http://www.morning.ndmbz.cn.gov.cn.ndmbz.cn http://www.morning.ptmch.com.gov.cn.ptmch.com http://www.morning.bsqkt.cn.gov.cn.bsqkt.cn http://www.morning.msgrq.cn.gov.cn.msgrq.cn http://www.morning.zfhwm.cn.gov.cn.zfhwm.cn http://www.morning.mfnsn.cn.gov.cn.mfnsn.cn http://www.morning.rwdbz.cn.gov.cn.rwdbz.cn http://www.morning.llxns.cn.gov.cn.llxns.cn