2016-12-14 102 views

回答

5
export class MyApp{ 
    constructor(public alert: AlertController,public platform: Platform){} 
    exit(){ 
     let alert = this.alert.create({ 
     title: 'Confirm', 
     message: 'Do you want to exit?', 
     buttons: [{ 
      text: "exit?", 
      handler:() => { this.exitApp() } 
     }, { 
      text: "Cancel", 
      role: 'cancel' 
     }] 
     }) 
     alert.present(); 
    } 
    exitApp(){ 
    this.platform.exitApp(); 
    } 
} 

如果您想启用后退按钮退出,请为其添加事件监听器并调用exit函数。

您可以使用this.platform.registerBackButtonAction(this.exit)

+1

退出偶数页推应用程序,它不应该退出,如果任何页面打开 – rashidnk

1

我可以通过自己找到合适的解决方案:

https://forum.ionicframework.com/t/show-a-confirmation-alert-before-app-close-ionic/63313

showedAlert: boolean; 

constructor(..., public alertCtrl: AlertController) { 
} 

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. 
     StatusBar.styleDefault(); 
     Splashscreen.hide(); 
     this.showedAlert = false; 

     // Confirm exit 
     this.platform.registerBackButtonAction(() => { 
      if (this.nav.length() == 1) { 
       if (!this.showedAlert) { 
        this.confirmExitApp(); 
       } else { 
        this.showedAlert = false; 
        this.confirmAlert.dismiss(); 
       } 
      } 

      this.nav.pop(); 
     }); 

    }); 
} 

confirmExitApp() { 
    this.showedAlert = true; 
    this.confirmAlert = this.alertCtrl.create({ 
     title: "Salir", 
     message: "¿ Esta seguro que desea salir de la aplicación ?", 
     buttons: [ 
      { 
       text: 'Cancelar', 
       handler:() => { 
        this.showedAlert = false; 
        return; 
       } 
      }, 
      { 
       text: 'Aceptar', 
       handler:() => { 
        this.platform.exitApp(); 
       } 
      } 
     ] 
    }); 
    this.confirmAlert.present(); 
} 
相关问题