HarmonyOS如何让页面的底部三个分栏对应三个不同的Page

一个界面底部有三个分栏,是用Tabs实现比较好还是用Navigation比较好? 这三个分栏的页面逻辑是会越来越庞大的,目前是用Tabs做的,三个分栏相当于三个TabContent,每个TabContent放一个自定义的Component,但是这三个TabContent是在一个page里面的,共享了一个page的生命周期,这样应该不好吧,如果希望三个Tab对应三个Page,这样该怎么做呢?

HarmonyOS
2024-08-30 16:28:29
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
put_get

可以自定义底部tabs内容,使用stack叠加在 Navigation 上面,简易demo如下:

@Entry 
@Component 
struct Index { 
  @State cIndex: number = 0 
  @State entryRouter: NavPathStack = new NavPathStack(); 
  @Builder 
  pageMap(name: string) { 
    if (name === 'PageA') { 
      PageA() 
    } else if (name === 'PageB') { 
      PageB() 
    } 
  } 
  @Builder 
  TabBuilders() { 
    Row() { 
      Text('PageA') 
        .fontColor(this.cIndex === 0 ? Color.Blue : Color.Black) 
        .fontWeight(500) 
        .onClick(()=>{ 
          this.cIndex = 0 
          this.entryRouter.pushPathByName('PageA','') 
        }) 
      Text('PageB') 
        .fontColor(this.cIndex === 1 ? Color.Blue : Color.Black) 
        .fontWeight(500) 
        .onClick(()=>{ 
          this.cIndex = 1 
          this.entryRouter.pushPathByName('PageB','') 
        }) 
    } 
    .width('100%') 
    .height('56vp') 
    .justifyContent(FlexAlign.SpaceAround) 
  } 
  aboutToAppear(): void { 
    this.entryRouter.pushPathByName('PageA', '') 
  } 
  build() { 
    Stack({alignContent:Alignment.Bottom}) { 
      Scroll() { 
        Navigation(this.entryRouter) { 
        } 
        .navDestination(this.pageMap) 
        .hideTitleBar(true) 
        .hideToolBar(false) 
      }.height('100%') 
 
      this.TabBuilders() 
    } 
    .height('100%') 
    .width('100%') 
    .backgroundColor(0xf0fff0) 
  } 
} 
@Component 
struct PageA { 
  aboutToAppear(): void { 
    console.log('this is pageA') 
  } 
  build() { 
    NavDestination() { 
      Column() { 
        Text('this is pageA') 
      } 
    }.hideTitleBar(true) 
  } 
} 
@Component 
struct PageB { 
  aboutToAppear(): void { 
    console.log('this is pageB') 
  } 
  build() { 
    NavDestination() { 
      Column() { 
        Text('this is pageB') 
      } 
    }.hideTitleBar(true) 
  } 
}
分享
微博
QQ
微信
回复
2024-09-02 16:25:43
相关问题
三个设备如何同时实现多端协同?
5093浏览 • 1回复 待解决
如何实现page页面的横竖屏切换
167浏览 • 1回复 待解决
HarmonyOS RN方库列表对应CAPI库列表
132浏览 • 1回复 待解决
ArkTS如何实现一底部弹窗?
316浏览 • 1回复 待解决
如何在本地引入一方har共享包?
262浏览 • 1回复 待解决
HarmonyOSUI 优化方面的问题
63浏览 • 1回复 待解决