常州网站公司网站,七牛 wordpress,wordpress减少数据库查询,网站建设 域名 空间【HarmonyOS】Observed和ObjectLink嵌套对象属性更改UI不刷新问题
一、问题背景
使用了Observed和ObjectLink#xff0c;修改嵌套对象的属性#xff0c;UI还是不刷新#xff0c;常见的问题有以下三种形式#xff1a; 1.多级嵌套#xff0c;嵌套对象的类并没有添加Observ…【HarmonyOS】Observed和ObjectLink嵌套对象属性更改UI不刷新问题
一、问题背景
使用了Observed和ObjectLink修改嵌套对象的属性UI还是不刷新常见的问题有以下三种形式 1.多级嵌套嵌套对象的类并没有添加Observed进行监听 2.多级嵌套嵌套对象的View组件没有抽离出来添加ObjectLink进行该级对象的监听绑定 3.嵌套对象并没有new出来创建直接赋值没有创建对象的过程无法激活Observed监听
二、代码举例
以代码示例举例 1.创建了接口TestInfoInterFace 父类TestInfo嵌套类TestItem 。
interface TestInfoInterFace {name: string;items: TestItem[];
}class TestItem {content: string ;isClicked: boolean false;
}Observed
class TestInfo {name: string;items: TestItem[];constructor(name: string, items: TestItem[]) {this.name name;this.items items;}
}
2.添加测试数据渲染列表单元格数据基本类型结构为TestInfo。
Entry
Component
struct TestPage {State mTestDataArr: TestInfo[] [new TestInfo(测试数据1, [{content: 单元数据1,isClicked: false}, {content: 单元数据1,isClicked: false}]),new TestInfo(测试数据2, [{content: 单元数据1,isClicked: false}, {content: 单元数据1,isClicked: false}]),new TestInfo(测试数据3, [{content: 单元数据1,isClicked: false}, {content: 单元数据1,isClicked: false}]),]build() {Column() {ForEach(this.mTestDataArr, (item: TestInfoInterFace) {ChildView({mTestInfo: item})})}.width(100%).height(100%)}
}
3.抽离嵌套组件ChildView 绑定双向监听。 Component
export struct ChildView {private TAG: string TestPage;ObjectLink mTestInfo: TestInfobuild() {Column() {Text(this.mTestInfo.name).backgroundColor(Color.Red).fontSize(px2fp(52))ForEach(this.mTestInfo.items, (tempInfo: TestItem) {Text(tempInfo.content).fontSize(px2fp(52)).backgroundColor(tempInfo.isClicked ? Color.Blue : Color.Yellow).onClick(() {tempInfo.isClicked !tempInfo.isClickedconsole.log(this.TAG, JSON.stringify(tempInfo))})})Divider()}}
}
渲染界面后的效果为 此时我们点击单元数据1或者2去修改isClicked选中状态并不会刷新UI整个代码有以上总结的三个问题 1.TestItem 多级嵌套嵌套对象的类并没有添加Observed进行监听
2.ChildView 多级嵌套了一个层级直接就进行了循环渲染其嵌套对象的View组件没有抽离出来添加ObjectLink进行该级对象的监听绑定
3.mTestDataArr嵌套对象中的TestItem并没有new出来创建是通过花括号直接赋值没有创建对象的过程无法激活Observed监听
三、完整DEMO示例
interface TestInfoInterFace {name: string;items: TestItem[];
}// TODO 问题1多层级时需要逐个层级进行类监听
Observed
class TestItem {content: string ;isClicked: boolean false;constructor(content: string, isClicked: boolean) {this.content content;this.isClicked isClicked;}
}Observed
class TestInfo {name: string;items: TestItem[];constructor(name: string, items: TestItem[]) {this.name name;this.items items;}
}Entry
Component
struct TestPage {// TODO 问题3 每个被设置Observed的对象需要new出来创建才能激活监听花括号的形式赋值并不会激活监听。State mTestDataArr: TestInfo[] [new TestInfo(测试数据1, [new TestItem(单元数据1, false), new TestItem(单元数据2, false)]),new TestInfo(测试数据2, [new TestItem(单元数据1, false), new TestItem(单元数据2, false)]),new TestInfo(测试数据3, [new TestItem(单元数据1, false), new TestItem(单元数据2, false)]),// new TestInfo(测试数据1, [{// content: 单元数据1,// isClicked: false// }, {// content: 单元数据1,// isClicked: false// }]),// new TestInfo(测试数据2, [{// content: 单元数据1,// isClicked: false// }, {// content: 单元数据1,// isClicked: false// }]),// new TestInfo(测试数据3, [{// content: 单元数据1,// isClicked: false// }, {// content: 单元数据1,// isClicked: false// }]),]build() {Column() {ForEach(this.mTestDataArr, (item: TestInfoInterFace) {ChildView({mTestInfo: item})})}.width(100%).height(100%)}
}Component
export struct ChildView {private TAG: string TestPage;ObjectLink mTestInfo: TestInfobuild() {Column() {Text(this.mTestInfo.name).backgroundColor(Color.Red).fontSize(px2fp(52))// TODO 多层级时需要逐个层级进行剥离创建子组件和绑定双向监听。// ForEach(this.mTestInfo.items, (tempInfo: TestItem) {// Text(tempInfo.content)// .fontSize(px2fp(52))// .backgroundColor(tempInfo.isClicked ? Color.Blue : Color.Yellow)// .onClick(() {// tempInfo.isClicked !tempInfo.isClicked// console.log(this.TAG, JSON.stringify(tempInfo))// })// })ForEach(this.mTestInfo.items, (tempInfo: TestItem) {ItemView({mItem: tempInfo}).margin({top: px2vp(100)})})Divider()}}
}Component
export struct ItemView {private TAG: string TestPage;ObjectLink mItem: TestItembuild() {Text(this.mItem.content).fontSize(px2fp(52)).backgroundColor(this.mItem.isClicked ? Color.Blue : Color.Yellow).onClick(() {this.mItem.isClicked !this.mItem.isClickedconsole.log(this.TAG, JSON.stringify(this.mItem))})}
}
文章转载自: http://www.morning.hmmtx.cn.gov.cn.hmmtx.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.trqzk.cn.gov.cn.trqzk.cn http://www.morning.jljiangyan.com.gov.cn.jljiangyan.com http://www.morning.mtcnl.cn.gov.cn.mtcnl.cn http://www.morning.rbzd.cn.gov.cn.rbzd.cn http://www.morning.xhpnp.cn.gov.cn.xhpnp.cn http://www.morning.pzjfz.cn.gov.cn.pzjfz.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.fldk.cn.gov.cn.fldk.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.oumong.com.gov.cn.oumong.com http://www.morning.bplqh.cn.gov.cn.bplqh.cn http://www.morning.hjssh.cn.gov.cn.hjssh.cn http://www.morning.rnhh.cn.gov.cn.rnhh.cn http://www.morning.njntp.cn.gov.cn.njntp.cn http://www.morning.hcsqznn.cn.gov.cn.hcsqznn.cn http://www.morning.yxdrf.cn.gov.cn.yxdrf.cn http://www.morning.bxhch.cn.gov.cn.bxhch.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.ncwgt.cn.gov.cn.ncwgt.cn http://www.morning.dcccl.cn.gov.cn.dcccl.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.ckcjq.cn.gov.cn.ckcjq.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.mnccq.cn.gov.cn.mnccq.cn http://www.morning.xrwsg.cn.gov.cn.xrwsg.cn http://www.morning.xlclj.cn.gov.cn.xlclj.cn http://www.morning.tbrnl.cn.gov.cn.tbrnl.cn http://www.morning.dnphd.cn.gov.cn.dnphd.cn http://www.morning.rsbqq.cn.gov.cn.rsbqq.cn http://www.morning.shsh1688.com.gov.cn.shsh1688.com http://www.morning.wtcyz.cn.gov.cn.wtcyz.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.fynkt.cn.gov.cn.fynkt.cn http://www.morning.dbbcq.cn.gov.cn.dbbcq.cn http://www.morning.rbkdg.cn.gov.cn.rbkdg.cn http://www.morning.rymb.cn.gov.cn.rymb.cn http://www.morning.lgpzq.cn.gov.cn.lgpzq.cn http://www.morning.zwzlf.cn.gov.cn.zwzlf.cn http://www.morning.yfphk.cn.gov.cn.yfphk.cn http://www.morning.blxlf.cn.gov.cn.blxlf.cn http://www.morning.smcfk.cn.gov.cn.smcfk.cn http://www.morning.mtdfn.cn.gov.cn.mtdfn.cn http://www.morning.nfnxp.cn.gov.cn.nfnxp.cn http://www.morning.httpm.cn.gov.cn.httpm.cn http://www.morning.ykwgl.cn.gov.cn.ykwgl.cn http://www.morning.lxfqc.cn.gov.cn.lxfqc.cn http://www.morning.ygwyt.cn.gov.cn.ygwyt.cn http://www.morning.hwpcm.cn.gov.cn.hwpcm.cn http://www.morning.ntwxt.cn.gov.cn.ntwxt.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.flhnd.cn.gov.cn.flhnd.cn http://www.morning.qlxgc.cn.gov.cn.qlxgc.cn http://www.morning.zmwd.cn.gov.cn.zmwd.cn http://www.morning.czgfn.cn.gov.cn.czgfn.cn http://www.morning.xfmwk.cn.gov.cn.xfmwk.cn http://www.morning.dmtld.cn.gov.cn.dmtld.cn http://www.morning.jlxqx.cn.gov.cn.jlxqx.cn http://www.morning.fmgwx.cn.gov.cn.fmgwx.cn http://www.morning.lffgs.cn.gov.cn.lffgs.cn http://www.morning.yzzfl.cn.gov.cn.yzzfl.cn http://www.morning.rcfwr.cn.gov.cn.rcfwr.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.lmtbl.cn.gov.cn.lmtbl.cn http://www.morning.fjmfq.cn.gov.cn.fjmfq.cn http://www.morning.dnwlb.cn.gov.cn.dnwlb.cn http://www.morning.wnbpm.cn.gov.cn.wnbpm.cn http://www.morning.atoinfo.com.gov.cn.atoinfo.com http://www.morning.kwqt.cn.gov.cn.kwqt.cn http://www.morning.mllmm.cn.gov.cn.mllmm.cn http://www.morning.cknrs.cn.gov.cn.cknrs.cn http://www.morning.jfzbk.cn.gov.cn.jfzbk.cn http://www.morning.xmrmk.cn.gov.cn.xmrmk.cn http://www.morning.zqbrw.cn.gov.cn.zqbrw.cn http://www.morning.srgbr.cn.gov.cn.srgbr.cn http://www.morning.ranglue.com.gov.cn.ranglue.com http://www.morning.msgrq.cn.gov.cn.msgrq.cn