首页又称为导航栏(NavBar),由三部分组成:标题栏、内容区(Navigation子组件)、工具栏;
子页则通过NavDestination承载,由两部分组成:标题栏、内容区(NavDestination子组件)。
返回首页一般存在两种方式:
直接在首页通过pushPathByName等方法跳转到PageOne页面,同时添加onPop回调接收PageOne页面返回的参数即可。
@Entry
@Component
struct NavPopSolution {
pathStack: NavPathStack = new NavPathStack();
@State message: string = '展示返回参数';
@Builder
pageMap(name: string) {
if (name === 'PageOne') {
PageOne();
}
}
build() {
Navigation(this.pathStack) {
Column({ space: 16 }) {
Text(this.message);
Button('跳转到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.pushPathByName('PageOne', '', (data) => {
// 用于页面出栈时触发该回调处理返回结果。
this.message = JSON.stringify(data.result);
});
});
};
}.title('首页')
.navDestination(this.pageMap);
}
}
@Component
struct PageOne {
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column() {
Button('携带参数回退首页', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.pop({ 'content': 'c' });
});
};
}.title('PageOne')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
});
}
} 效果预览:

由于需要立马返回首页,无法一步步将路由栈中页面逐个出栈,所以无法通过出栈时的onPop回调,拿到上一个页面出栈时携带的参数。且首页无法推入路由栈,不能使用push类方法跳转,也没有onReady生命周期,所以也无法在onReady接收其他子页的传参。
现提供两种方式完成跳转,并将参数携带回首页。
@Entry
@Component
struct NavMainPageExample {
pathStack: NavPathStack = new NavPathStack();
aboutToAppear() {
// 将MainPage设置成自定义首页
this.pathStack.pushPathByName('MainPage', '', false);
// 设置Navigation页面跳转拦截回调
this.pathStack.setInterception({
willShow: (from: NavDestinationContext | 'navBar', to: NavDestinationContext | 'navBar',
operation: NavigationOperation, animated: boolean) => {
console.info(`${from} ${to} ${operation} ${animated}`);
// 如果要返回到首页,就push名为MainPage的子页面
if (to === 'navBar') {
this.pathStack.pushPathByName('MainPage', '', false);
}
}
});
}
@Builder
pageMap(name: string) {
if (name === 'MainPage') {
MainPage();
} else if (name === 'PageOne') {
PageOne();
} else if (name === 'PageTwo') {
PageTwo();
}
}
build() {
Navigation(this.pathStack) {
}.hideNavBar(true) // 隐藏首页
.navDestination(this.pageMap);
}
}
// NavDestination作为首页
@Component
struct MainPage {
@State message: string = '';
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column({ space: 16 }) {
Text(this.message);
Button('跳转到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.pushPathByName('PageOne', null, true);
});
};
}.title('MainPage')
.onReady((ctx: NavDestinationContext) => {
this.pathStack = ctx.pathStack;
this.message = JSON.stringify(this.pathStack.getParamByName('MainPage'));
});
}
}
@Component
export struct PageOne {
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column() {
Button('跳转到PageTwo', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.pushPathByName('PageTwo', null, true);
});
};
}.title('PageOne')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
});
}
}
@Component
struct PageTwo {
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column() {
Button('携带参数回退首页', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.clear(); // 清除路由栈
this.pathStack.pushPath({ name: 'MainPage', param: 'context' }); // 跳转到自定义首页
});
};
}.title('PageTwo')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
});
}
}
连续调用多个页面栈操作方法时,中间过程会被忽略,显示最终的栈操作结果。
效果预览:

import emitter from '@ohos.events.emitter';
@Entry
@Component
struct EmitterSolution {
pathStack: NavPathStack = new NavPathStack();
@State message: emitter.EventData = {};
aboutToAppear() {
// 监听back事件,获取参数
emitter.on('back', (data) => {
this.message = data;
});
}
aboutToDisappear() {
emitter.off('back'); // 取消监听
}
@Builder
pageMap(name: string) {
if (name === 'PageOne') {
PageOne();
} else if (name === 'PageTwo') {
PageTwo();
}
}
build() {
Navigation(this.pathStack) {
Column({ space: 16 }) {
Text(JSON.stringify(this.message));
Button('跳转到PageOne', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.pushPathByName('PageOne', null, true);
});
};
}.title('首页')
.navDestination(this.pageMap);
}
}
@Component
struct PageOne {
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column() {
Button('跳转到PageTwo', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.pushPathByName('PageTwo', null, true);
});
};
}.title('PageOne')
.onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
});
}
}
@Component
struct PageTwo {
pathStack: NavPathStack = new NavPathStack();
build() {
NavDestination() {
Column() {
Button('回退到首页,通过emitter传递参数', { stateEffect: true, type: ButtonType.Capsule })
.onClick(() => {
this.pathStack.clear();
let eventData: emitter.EventData = { data: { 'content': 'c', 'id': '1' } };
emitter.emit('back', eventData); // 传递back事件给首页
});
};
}.title('PageTwo').onReady((context: NavDestinationContext) => {
this.pathStack = context.pathStack;
});
}
} 效果预览:
