如何实现根据服务端下发的配置动态加载tab页面?

app注册了若干页面 pages/pageA pages/pageB pages/pageC ... app首页为4个page组成的tab页面,如何实现根据服务端下发的配置动态加载tab页面,类似于页面跳转时router.pushUrl传入字符串,tab页面能否传入4个字符串加载对应的4个page。

HarmonyOS
2024-10-09 09:59:03
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
zxjiu

可以通过将tab中currentIndex设置为全局状态,以实现效果。参考如下demo:

//OtherPage页面  
import router from '@ohos.router'  
  
@Entry  
@Component  
struct OtherPage {  
  
  build() {  
    Row() {  
      Column() {  
        Button("去首页")  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(()=>{  
            AppStorage.setOrCreate("IndexPage", 0)  
            router.pushUrl({url:'pages/MainPage'})  
          })  
  
        Button("去第二页")  
          .fontSize(50)  
          .fontWeight(FontWeight.Bold)  
          .onClick(()=>{  
            AppStorage.setOrCreate("IndexPage", 1);  
            router.pushUrl({url:'pages/MainPage'})  
          })  
      }  
      .width('100%')  
    }  
    .height('100%')  
  }  
}  
  
//MainPage页面  
@Entry  
@Component  
struct MainPage {  
  @StorageLink("IndexPage") currentIndex: number = 0  
  private controller: TabsController = new TabsController()  
  
  @Builder  
  tabBuilder(index: number, name: string) {  
    Column() {  
      Text(name).fontSize(22)  
      Divider()  
        .strokeWidth(2)  
        .color('#007DFF')  
        .opacity(this.currentIndex === index ? 1 : 0)  
    }  
  }  
  
  build() {  
    Tabs({ barPosition: BarPosition.End, index:this.currentIndex,controller:this.controller}) {  
  
      TabContent() {  
      }  
      .backgroundColor(Color.Yellow)  
      .tabBar(this.tabBuilder(0, 'yellow'))  
  
      TabContent() {  
      }  
      .backgroundColor(Color.Green)  
      .tabBar(this.tabBuilder(1, 'green'))  
  
  
    }  
    .onChange((index: number) => {  
      this.currentIndex = 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.

相关api文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-appstorage-V5

分享
微博
QQ
微信
回复
2024-10-09 16:17:18
相关问题
List上展示服务端下发HTML字符串
730浏览 • 1回复 待解决
HarmonyOS如何根据条件加载页面
902浏览 • 1回复 待解决
HarmonyOS 页面动态配置
619浏览 • 1回复 待解决
是否支持模块动态加载如何实现
3261浏览 • 1回复 待解决
原子化服务动态加载与热更新
271浏览 • 0回复 待解决
如何实现小说插件、直播插件下发
2869浏览 • 1回复 待解决
如何在HarmonyOS中实现动态加载模块?
1220浏览 • 1回复 待解决
如何动态根据资源名获取资源id
9557浏览 • 1回复 已解决