HarmonyOS @Builder修饰的方法中参数属性更新无法触发页面重绘
// xxx.ets
@Entry
@Component
struct TabsExample {
@State fontColor: string = '#182431'
@State selectedFontColor: string = '#007DFF'
@State currentIndex: number = 0
private controller: TabsController = new TabsController()
@State tab0: TabModel = new TabModel("green", 0)
@State tab1: TabModel = new TabModel("blue", 1)
@State tab2: TabModel = new TabModel("yellow", 2)
@State tab3: TabModel = new TabModel("pink", 3)
aboutToAppear(): void {
setTimeout(() => {
this.tab0.title = "green 222"
this.tab1.title = "blue 222"
this.tab2.title = "yellow 222"
this.tab3.title = "pink 222"
}, 2000)
}
@Builder
tabBuilder(tabModel: TabModel) {
Column() {
Text(tabModel.title)
.fontColor(this.currentIndex === tabModel.index ? this.selectedFontColor : this.fontColor)
.fontSize(16)
.fontWeight(this.currentIndex === tabModel.index ? 500 : 400)
.lineHeight(22)
.margin({ top: 17, bottom: 7 })
.onAreaChange(() => {
})
Divider()
.strokeWidth(2)
.color('#007DFF')
.opacity(this.currentIndex === tabModel.index ? 1 : 0)
}
}
build() {
Column() {
Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
TabContent() {
Column() {
Text(this.tab0.title)
}.width('100%').height('100%').backgroundColor('#00CB87')
}
.tabBar(this.tabBuilder(this.tab0))
TabContent() {
Column().width('100%').height('100%').backgroundColor('#007DFF')
}.tabBar(this.tabBuilder(this.tab1))
TabContent() {
Column().width('100%').height('100%').backgroundColor('#FFBF00')
}.tabBar(this.tabBuilder(this.tab2))
TabContent() {
Column().width('100%').height('100%').backgroundColor('#E67C92')
}.tabBar(this.tabBuilder(this.tab3))
}
.vertical(false)
.scrollable(false)
.barMode(BarMode.Fixed)
.barWidth('100%')
.barHeight(56)
.animationDuration(0)
.onChange((index: number) => {
this.currentIndex = index
})
.width(360)
.height('100%')
.margin({ top: 52 })
.backgroundColor('#F1F3F5')
}.width('100%')
}
}
@Observed
export class TabModel {
title: string = ''
index: number = 0
constructor(title: string, index: number) {
this.title = title
this.index = index
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
延时2秒更新tab标题未生效,需要手动切换tab才生效。实际业务中需要在网络请求完成后更新tab标题,这里用延时模拟。
HarmonyOS
赞
收藏 0
回答 1
相关问题
HarmonyOS Canvas如何触发重绘
1230浏览 • 1回复 待解决
HarmonyOS @entry 修饰的页面,onBackPress方法不触发
1052浏览 • 1回复 待解决
backgroud()方法使用@State修饰的状态变量值更新后未能触发不同的@builder方法刷新
2955浏览 • 1回复 待解决
HarmonyOS 如何让Canvas重绘
660浏览 • 1回复 待解决
#鸿蒙通关秘籍#如何优化HarmonyOS滑动页面组件的重绘性能?
943浏览 • 1回复 待解决
HarmonyOS 是否有重绘、重排的相关资料
664浏览 • 1回复 待解决
HarmonyOS 想要通过@State修饰的变量刷新@Builder方法内的UI,只能通过引用传递参数吗?
998浏览 • 1回复 待解决
为什么@Link修饰的数组类型直接调用push方法可能不触发UI更新?
181浏览 • 1回复 待解决
HarmonyOS 自定义builder方法, 参数是按引用传递, 当状态值修改时, 不触发builder方法, 不会刷新UI
1094浏览 • 1回复 待解决
builder修饰的快速组件化方法能使用Button吗
2465浏览 • 0回复 待解决
HarmonyOS 组件状态变量改变,build重绘问题
714浏览 • 1回复 待解决
HarmonyOS 关于组件重绘的生命周期函数
912浏览 • 1回复 待解决
@State修饰的状态数据被修改时会触发组件的什么方法进行UI界面更新
135浏览 • 1回复 待解决
HarmonyOS 如何使登录成功后强制重绘页面,而不是依赖@state
393浏览 • 1回复 待解决
HarmonyOS 自定义布局onPlaceChildren何时被重绘
416浏览 • 1回复 待解决
HarmonyOS builder 作为 builder 的参数传递
776浏览 • 1回复 待解决
Map中可以存入@Builder修饰的构造函数吗
2033浏览 • 1回复 待解决
HarmonyOS有没有回调函数能够在组件重绘的时候调用?
886浏览 • 1回复 待解决
HarmonyOS @Builder 修饰的api中,设置圆角不起作用
837浏览 • 1回复 待解决
HarmonyOS @BuilderParam组件作为参数传递后自身引用的@State变量无法触发UI布局更新
525浏览 • 1回复 待解决
HarmonyOS @Builder装饰的方法可以作为参数传递吗
626浏览 • 1回复 待解决
HarmonyOS 使用@builder和@builderParam时,@builder中的ui不会动态更新
1388浏览 • 1回复 待解决
HarmonyOS @Observed修饰的class,当内部属性变化时UI不更新
718浏览 • 1回复 待解决
HarmonyOS 在@builder中使用引用传递无法触发UI刷新
525浏览 • 1回复 待解决
参考示例如下:
可以参考如下链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builder-V5#按引用传递参数