复杂页面开发的场景下,在开发中精准控制组件更新?

复杂页面开发的场景下,在开发中精准控制组件更新?

HarmonyOS
2024-08-06 14:01:16
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
chmqn

将属性进行拆分,将一个大的属性对象拆分成几个小的属性对象,来减少甚至避免冗余刷新的现象,达到精准控制组件的更新范围@Observed

class ClassB {
subProp1: number = 100;
}
@Observed
class ClassA {
prop1: number = 0;
prop2: string = "This is Prop2";
prop3: ClassB = new ClassB();
}
@Component
struct CompA {
@ObjectLink a: ClassA;
private sizeFont: number = 30; // the private variable does not invoke rendering
private isRenderText() : number {
this.sizeFont++; // the change of sizeFont will not invoke rendering, but showing that the function is called
console.info("Text prop2 is rendered");
return this.sizeFont;
}
build() {
Column() {
Text(this.a.prop2) // when this.a.prop1 changes, it will invoke Text rerendering
.margin({ bottom: 10 })
.fontSize(this.isRenderText()) //if the Text renders, the function isRenderText will be called
Text("subProp1 : " + this.a.prop3.subProp1) //the Text can not observe the change of subProp1
.fontSize(30)
}
}
}
@Entry
@Component
struct Page {
@State a: ClassA = new ClassA();
build() {
Row() {
Column() {
Text("Prop1: " + this.a.prop1)
.margin({ bottom: 20 })
.fontSize(50)
CompA({a: this.a})
Button("Change prop1")
.width(200)
.fontSize(20)
.backgroundColor("#FF007DFF")
.margin({
top: 10,
bottom: 10
})
.onClick(() => {
this.a.prop1 = this.a.prop1 + 1 ;
})
Button("Change subProp1")
.width(200)
.fontSize(20)
.backgroundColor("#FF007DFF")
.onClick(() => {
this.a.prop3.subProp1 = this.a.prop3.subProp1 + 1;
})
}
.width('100%')
}
.width('100%')
.height('100%')
}
}
分享
微博
QQ
微信
回复
2024-08-06 19:50:31
相关问题
HarmonyOS如何控制组件属性输出
144浏览 • 1回复 待解决
用数组变量控制组件属性不生效
1501浏览 • 1回复 待解决
【持续更新】智能表开发疑惑
7793浏览 • 3回复 待解决
图形图像开发场景实践
473浏览 • 1回复 待解决
访问HSP包ArkUI组件访问与开发
598浏览 • 1回复 待解决
关于api8ets开发引用xml图片问题
3059浏览 • 1回复 待解决
BLE蓝牙开发如何实现对智能灯控制?
6519浏览 • 1回复 待解决