前提:懒加载的app,非懒加载的没有试过
ionic3 + angular5 项目
登录界面(rootPage):
backButtonPressed: any;
constructor(
private platform: Platform,
public toastCtrl: ToastController,
public appCtrl: App,
private alertCtrl: AlertController, ) {
console.log(window.location.href.indexOf('login') > -1, 'href');
platform.ready().then(() => {
platform.registerBackButtonAction(() => {
this.showExit();
})
})
}
//退出应用方法
showExit(msg?: string): void {
//如果为true,退出
if (this.backButtonPressed) {
this.platform.exitApp();
} else {
//第一次按,弹出Toast
this.toastCtrl.create({
message: msg ? msg : '再按一次退出应用',
duration: 2000,
position: 'top'
}).present();
//标记为true
this.backButtonPressed = true;
//两秒后标记为false,如果退出的话,就不会执行了
setTimeout(() => this.backButtonPressed = false, 2000);
}
}
其它需要注册自定义方法的界面:
backButtonPressed: any;
constructor(
private platform: Platform,
public appCtrl: App,
private alertCtrl: AlertController, ) {
platform.ready().then(() => {
platform.registerBackButtonAction(() => {
let activeNav: NavController = this.appCtrl.getActiveNav();
setTimeout(() => {
if (window.location.href.indexOf('work-center') > -1 || window.location.href.indexOf('login') > -1) { //根据路由判断,是否是需要注册自定义方法的页面
this.showExit(); //退出
} else {
activeNav.pop(); //返回上一页
}
})
})
})
}