在 Ionic 2 中从侧面导航菜单切换应用程序页面时,后退按钮不可用



我正在开发一个 ionic 2 移动应用程序,我发现当我通过侧面导航面板访问页面时,它没有在特定页面上提供自动生成的后退按钮,而当我以正常遍历形式逐页访问页面时,该页面可以使用后退按钮。我如何从侧面导航面板菜单中获取后退按钮,请建议。

<ion-header>
<ion-navbar color="primary">
<button ion-button menuToggle>
<ion-icon name="menu"></ion-icon>
</button>
<ion-title>dashboard</ion-title>
</ion-navbar>

如果您在创建项目时创建了侧菜单应用程序,则可以在app.component.ts中看到以下代码

constructor(public platform: Platform, public statusBar: StatusBar, public splashScreen: SplashScreen) {
this.initializeApp();
// used for an example of ngFor and navigation
this.pages = [
{ title: 'Home', component: Createpage}
];
}
initializeApp() {
this.platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
this.statusBar.styleDefault();
this.splashScreen.hide();
});
}
openPage(page) {
// Reset the content nav to have just this page
this.nav.setRoot(page.component);
}

正如您在OpenPage函数中看到的那样,它将页面组件设置为root,因此您看不到后退按钮,而是看到汉堡图标,该图标打开侧边菜单

现在

如果您仍然希望使用后退按钮从侧边菜单打开页面,您可以执行以下操作

openPage(page) {
// Reset the content nav to have just this page
// we wouldn't want the back button to show in this scenario
if(page.component == HomePage){
this.nav.setRoot(HomePage);
} else {
this.nav.push(page.component);
}
}

在上面的代码中,您检查它是否是后退按钮组件(您希望使用后退按钮打开(,如果这是真的,您可以将其设置为push而不是setRoot

评论以获得更多说明

最新更新