OpenHarmony UI组件更新与维护实践
一 更新策略与版本管理
二 组件更新流程与关键注意点
三 维护机制与性能优化
四 常见维护清单与示例模板
@Component
@Reusable
export struct CounterCard {
@Prop count: number = 0
@State total: number = 0
@Watch('onCountUpdated')
onCountUpdated(propName?: string) {
// 快速计算,避免修改自身监听的状态,避免 async/await
this.total = this.count * 2
}
build() {
Column({ space: 8 }) {
Text(`Count: ${this.count}`).fontSize(16)
Text(`Total: ${this.total}`).fontSize(16)
}
.padding(12)
.backgroundColor(Color.White)
.borderRadius(8)
}
}
@Entry
@Component
struct Page {
@State items: number[] = [1, 2, 3]
build() {
Column({ space: 12 }) {
ForEach(this.items, (n: number) => {
CounterCard({ count: n })
}, (n: number) => n.toString())
Button('Add')
.onClick(() => {
// 仅影响新增卡片的局部状态,避免全局重绘
this.items.push(this.items.length + 1)
})
}
.padding(16)
}
}
@Component
struct LifecycleAware {
aboutToAppear() {
// 订阅事件、启动定时器等
}
aboutToDisappear() {
// 取消订阅、清理定时器、释放资源
}
build() { /* ... */ }
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。