HarmonyOS鸿蒙Next中Navigation可以嵌套Navigation吗?

HarmonyOS鸿蒙Next中Navigation可以嵌套Navigation吗? 如果允许多层嵌套,

例如首页底部4个tab,也有其他的详情page,如何实现切换?(主要是不同层级的Navigation切换)

点tab,是内部Navigation的变化,点其他的是外部的Navigation跳转

3 回复

//Index.ets import { PageOneTmp } from ‘./PageOne’ import { pageTwoTmp } from ‘./PageTwo’ import { Pages } from ‘./PageTwo’ @Entry @Component struct TabsExample { @State fontColor: string = ‘#182431’ @State selectedFontColor: string = ‘#007DFF’ @State currentIndex: number = 0 private controller: TabsController = new TabsController() @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%’) } @Provide(‘pageInfos’) pageInfos: NavPathStack = new NavPathStack() @Builder PageMap(name: string) { if (name === ‘pageOne’) { PageOneTmp() } else if (name === ‘pageTwo’) { pageTwoTmp({ names: name, values: this.pageInfos } as Pages) } } build() { Column() { Navigation(this.pageInfos) { Column() { Tabs({ barPosition: BarPosition.End, index: this.currentIndex, controller: this.controller }) { TabContent() { Column() { Button(‘pushPath’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.pushPath({ name: ‘pageOne’ }) //将name指定的NavDestination页面信息入栈 }) } .width(‘100%’) .height(‘100%’) .backgroundColor(’#ff8aa941’) } .tabBar(this.tabBuilder(0, ‘green’)) TabContent() { Column() { Button(‘pushPath2’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.pushPath({ name: ‘pageOne’ }) //将name指定的NavDestination页面信息入栈 }) } .width(‘100%’) .height(‘100%’) .backgroundColor(’#007DFF’) } .tabBar(this.tabBuilder(1, ‘blue’)) TabContent() { Column() .width(‘100%’) .height(‘100%’) .backgroundColor(’#FFBF00’) } .tabBar(this.tabBuilder(2, ‘yellow’)) TabContent() { Column() .width(‘100%’) .height(‘100%’) .backgroundColor(’#E67C92’) } .tabBar(this.tabBuilder(3, ‘pink’)) } .vertical(false) .barMode(BarMode.Fixed) .barWidth(360) .barHeight(56) .animationDuration(400) .onChange((index: number) => { this.currentIndex = index }) .width(360) .height(‘100%’) .backgroundColor(’#F1F3F5’) } } .title(‘NavIndex’).navDestination(this.PageMap) .onNavBarStateChange((isVisible: boolean) => { console.info('test: == ’ + isVisible + '; currentIndex = ’ + this.currentIndex); }); } .width(‘100%’) } }

//PageOne.ets class TmpClass { count: number = 10 } @Component export struct PageOneTmp { @Consume(‘pageInfos’) pageInfos: NavPathStack; build() { NavDestination() { Column() { Button(‘pushPathByName’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { let tmp = new TmpClass() this.pageInfos.pushPathByName(‘pageTwo’, tmp) //将name指定的NavDestination页面信息入栈,传递的数据为param }) Button(‘popToname’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.popToName(‘pageTwo’) //回退路由栈到第一个名为name的NavDestination页面 console.log(‘popToName’ + JSON.stringify(this.pageInfos), ‘返回值’ + JSON.stringify(this.pageInfos.popToName(‘pageTwo’))) }) Button(‘popToIndex’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.popToIndex(1) // 回退路由栈到index指定的NavDestination页面 console.log(‘popToIndex’ + JSON.stringify(this.pageInfos)) }) Button(‘moveToTop’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.moveToTop(‘pageTwo’) // 将第一个名为name的NavDestination页面移到栈顶 console.log(‘moveToTop’ + JSON.stringify(this.pageInfos), ‘返回值’ + JSON.stringify(this.pageInfos.moveToTop(‘pageTwo’))) }) Button(‘moveIndexToTop’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.moveIndexToTop(1) // 将index指定的NavDestination页面移到栈顶 console.log(‘moveIndexToTop’ + JSON.stringify(this.pageInfos)) }) Button(‘clear’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { this.pageInfos.clear() //清除栈中所有页面 }) Button(‘get’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20).onClick(() => { console.log(’-------------------’) console.log(‘获取栈中所有NavDestination页面的名称’, JSON.stringify(this.pageInfos.getAllPathName())) console.log(‘获取index指定的NavDestination页面的参数信息’, JSON.stringify(this.pageInfos.getParamByIndex(1))) console.log(‘获取全部名为name的NavDestination页面的参数信息’, JSON.stringify(this.pageInfos.getParamByName(‘pageTwo’))) console.log(‘获取全部名为name的NavDestination页面的位置索引’, JSON.stringify(this.pageInfos.getIndexByName(‘pageOne’))) console.log(‘获取栈大小’, JSON.stringify(this.pageInfos.size())) }) } .width(‘100%’) .height(‘100%’) .backgroundColor(Color.Pink) .onBackPressed(() => { const popDestinationInfo = this.pageInfos.pop() // 弹出路由栈栈顶元素 console.log(‘pop’ + ‘返回值’ + JSON.stringify(popDestinationInfo)) return true }) } .title(‘pageOne’) .width(‘100%’) .height(‘100%’) .backgroundColor(Color.Pink) .onBackPressed(() => { return true }) } }

//PageTwo.ets export class Pages { names: string = “” values: NavPathStack | null = null } @Builder export function pageTwoTmp(info: Pages) { NavDestination() { Column() { Button(‘pushPathByName’, { stateEffect: true, type: ButtonType.Capsule }) .width(‘80%’) .height(40) .margin(20) .onClick(() => { (info.values as NavPathStack).pushPathByName(‘pageOne’, null) }) } .width(‘100%’) .height(‘100%’) } .title(‘pageTwo’) .onBackPressed(() => { (info.values as NavPathStack).pop() return true }) }

更多关于HarmonyOS鸿蒙Next中Navigation可以嵌套Navigation吗?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,Navigation组件是可以嵌套使用的。Navigation组件用于管理页面之间的导航关系,支持在同一个页面中嵌套多个Navigation组件,以实现更复杂的导航结构。每个Navigation组件可以独立管理自己的页面栈,并且可以在不同的Navigation组件之间进行页面的跳转和返回操作。这种嵌套使用的方式可以满足多层级导航的需求,提升应用的灵活性和用户体验。

在HarmonyOS鸿蒙Next中,Navigation组件支持嵌套使用。你可以在一个Navigation组件内部再嵌入另一个Navigation组件,以实现更复杂的页面导航结构。这种嵌套方式可以帮助你更好地组织和管理页面的层次关系,提升用户体验。但需要注意,过度嵌套可能会导致性能问题,因此建议合理规划导航结构。

回到顶部