HarmonyOS 未登录的情况下,如何控制Tabs中部分TabContent不被初始化

HarmonyOS
2024-12-26 15:40:50
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
zbw_apple

需求是用户只有登录才显示所有的TabContent的吗?可以设置个boolean变量isLogin,渲染时true就将该TabContent放开,未登录就不渲染,参考下面demo:

@Entry
@Component
struct TabsExample {
  @State fontColor: string = '#182431'
  @State selectedFontColor: string = '#007DFF'
  @State currentIndex: number = 0
  private controller: TabsController = new TabsController()
  @State isLogin: boolean = false
  @Builder tabBuilder(index: number, name: string) {
    Column() {
      Text(name)
        .fontColor(this.currentIndex === index ? this.selectedFontColor : this.fontColor)
        .fontSize(16)
        .fontWeight(this.currentIndex === index ? 500 : 400)
        .lineHeight(22)
        .margin({ top: 17, bottom: 7 })
      Divider()
        .strokeWidth(2)
        .color('#007DFF')
        .opacity(this.currentIndex === index ? 1 : 0)
    }.width('100%')
  }
  build() {
    Column() {
      Tabs({ barPosition: BarPosition.Start, index: this.currentIndex, controller: this.controller }) {
        TabContent() {
          Column(){
            Button("登录").onClick(() =>{
              this.isLogin = true
            })
            Button("退出登录").onClick(() =>{
              this.isLogin = false
            })
          }.width('100%').height('100%')
        }.tabBar(this.tabBuilder(0, 'green'))
        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#007DFF')
        }.tabBar(this.tabBuilder(1, 'blue'))
        TabContent() {
          Column().width('100%').height('100%').backgroundColor('#FFBF00')
        }.tabBar(this.tabBuilder(2, 'yellow'))
        if(this.isLogin){
          TabContent() {
            Column().width('100%').height('100%').backgroundColor('#E67C92')
          }.tabBar(this.tabBuilder(3, '登录显示'))
        }
      }
      .vertical(false)
      .barMode(BarMode.Fixed)
      .barWidth(360)
      .barHeight(56)
      .animationDuration(400)
      .onChange((index: number) => {
        this.currentIndex = index
      })
      .width(360)
      .height(296)
      .margin({ top: 52 })
      .backgroundColor('#F1F3F5')
    }.width('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-26 17:07:33
相关问题
HarmonyOS 导出类使用时报初始化
390浏览 • 1回复 待解决
HarmonyOS XComponent初始化
594浏览 • 1回复 待解决
HarmonyOS struct初始化方法
506浏览 • 1回复 待解决
HarmonyOS 组件初始化参数
262浏览 • 1回复 待解决
HarmonyOS Component初始化问题
945浏览 • 1回复 待解决
HarmonyOS export struct初始化参数
549浏览 • 1回复 待解决
初始化安全相机方法
870浏览 • 1回复 待解决
HarmonyOS 初始化EGL环境失败
426浏览 • 1回复 待解决
HarmonyOS 如何进行全局初始化操作
427浏览 • 1回复 待解决
HarmonyOS 如何动态对Class进行初始化
407浏览 • 1回复 待解决
HarmonyOS泛型类属性如何初始化
870浏览 • 1回复 待解决
HarmonyOS 应用RDB初始化示例
815浏览 • 1回复 待解决
HarmonyOS 推送服务初始化失败
263浏览 • 1回复 待解决
没法调试“已初始化”分支
565浏览 • 1回复 待解决
HarmonyOS 怎么监听Web初始化完成?
497浏览 • 1回复 待解决