HarmonyOS tabs组件异步赋值,tabBar不展示,tabContent正常展示?

示例代码:

interface newRepeatItem {
  item: string,
  index: number
}

@State newTabsList: Array<string> = []
Tabs({ barPosition: BarPosition.Start }) {
  Repeat(this.newTabsList)
    .each((obj: newRepeatItem) => {
      TabContent() {
        Text(obj.item)
      }.width('100%')
      .height('100%')
      .backgroundColor(Color.Brown)
      .tabBar(obj.item)

    })
    .key((item: string, index: number) => index.toString())      }
.onChange((index: number) => {
  this.currentIndex = index
})

.barHeight(50)
.barMode(BarMode.Scrollable)
}
setTimeout(() => {
  this.newTabsList.push('关注1')
  this.newTabsList.push('关注2')
  this.newTabsList.push('关注3')
}, 1000)
阅读 620
1 个回答

请参考demo:

@Entry
@Component
struct Index {
  @State tabArray: Array<number> = [0, 1, 2]
  @State focusIndex: number = 0
  @State index: number = 0
  private controller: TabsController = new TabsController()
  @State test: boolean = false
  // 单独的页签
  @Builder Tab(tabName: string, tabItem: number, tabIndex: number) {
    Row({ space: 20 }) {
      Text(tabName).fontSize(18)
    }
    .justifyContent(FlexAlign.Center)
    .constraintSize({ minWidth: 35 })
    .width('33.3%')
    .height(30)
    .borderRadius({ topLeft: 10, topRight: 10 })
    .onClick(() => {
      this.controller.changeIndex(tabIndex)
      this.focusIndex = tabIndex
    })
    .backgroundColor(tabIndex === this.focusIndex ? "#ffffffff" : "#ffb7b7b7")
  }
  build() {
    Column() {
      Column() {
        // 页签
        Row({ space: 7 }) {
          Row() {
            ForEach(this.tabArray, (item: number, index: number) => {
              this.Tab("页签 " + item, item, index)
            })
          }
          .justifyContent(FlexAlign.Start)
        }
        .width('80%')
        .backgroundColor("#ffb7b7b7")
        Tabs({ barPosition: BarPosition.Start, controller: this.controller }) {
          ForEach(this.tabArray, (item: number, index: number) => {
            TabContent() {
              Text('我是页面 ' + item + " 的内容")
                .height(300)
                .width('100%')
                .fontSize(30)
            }
          })
        }
        .animationDuration(100)
        .onChange((index: number) => {
          this.focusIndex = index
        })
      }
      .alignItems(HorizontalAlign.Start)
      .width('100%')
    }
    .height('100%')
  }
}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进