其实也很简单。大概分为三步
1.tabs.ts文件需要声明一下
@ViewChild('mainTabs') tabs:Tabs;//声明tabs组件(<ion-tabs #mainTabs >)
2.app.component.ts文件
@ViewChild('myNav') nav: Nav;//声明根组件(<ion-nav #myNav [root]="rootPage">)
3.贴代码
platform.ready().then(()=>{
this.exitApp();
})
public backButtonPressed: boolean = false;
exitApp() {
this.platform.registerBackButtonAction(() => {
//控制modal、系统自带提示框
let overlay = this.appCtrl._appRoot._overlayPortal.getActive() || this.appCtrl._appRoot._modalPortal.getActive();
if (overlay) {
overlay.dismiss();
return;
}
let activeVC = this.nav.getActive();
let page = activeVC.instance;
if (page.tabs) {
let activeNav = page.tabs.getSelected();
if (activeNav.canGoBack()) {
return activeNav.pop();
} else {
return this.showExit();
}
}
if (page instanceof LoginPage) {//查看当前页面是否是登陆页面
this.showExit();
return;
}
this.appCtrl.getActiveNav().pop();//剩余的情况全部使用全局路由进行操作
});
}
//双击退出函数
showExit() {
if (this.backButtonPressed) {
this.platform.exitApp();
} else {
this.presentToast();//再按一次退出
this.backButtonPressed = true;
setTimeout(() => {
this.backButtonPressed = false;
}, 2000)
}
}
presentToast() {
let toast = this.toastCtrl.create({
message: '再按一次退出应用',
duration: 2000,
position: 'top'
});
toast.onDidDismiss(() => {
console.log('Dismissed toast');
});
toast.present();
}
ionic3中点击退出app程序
最新推荐文章于 2020-10-21 11:31:55 发布