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
2025-01-09 17:03:25
1814浏览
收藏 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
  }
}
  • 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.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.

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

分享
微博
QQ
微信
回复
2025-01-09 19:47:04


相关问题
HarmonyOS Canvas如何触发
1230浏览 • 1回复 待解决
HarmonyOS 如何让Canvas
660浏览 • 1回复 待解决
HarmonyOS builder 作为 builder 参数传递
776浏览 • 1回复 待解决