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
  }
}

延时2秒更新tab标题未生效,需要手动切换tab才生效。实际业务中需要在网络请求完成后更新tab标题,这里用延时模拟。

HarmonyOS
1天前
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

参考示例如下:

class Tmp {
  param: TabModel = new TabModel("", 0)
}

@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(p: Tmp) {
    Column() {
      Text(p.param.title)
        .fontColor(this.currentIndex === p.param.index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === p.param.index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
        .onAreaChange(() => {

        })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')

        .opacity(this.currentIndex === p.param.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({
          param: this.tab0
        }))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.tabBuilder({
          param: this.tab1
        }))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.tabBuilder({
          param: this.tab2
        }))

        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#E67C92')
        }.tabBar(this.tabBuilder({
          param: 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
  }
}

可以参考如下链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-builder-V5#按引用传递参数

分享
微博
QQ
微信
回复
1天前
相关问题
HarmonyOS Canvas如何触发
438浏览 • 1回复 待解决
HarmonyOS 如何让Canvas
34浏览 • 1回复 待解决
HarmonyOS builder 作为 builder 参数传递
169浏览 • 1回复 待解决
@BuilderParam修饰属性报错
1921浏览 • 1回复 待解决