2017-10-10 74 views
0

我尝试在我的离子应用程序中覆盖手机的后退按钮。离子3:用手机的后退按钮关闭模式

此代码允许我打开一个模式来关闭应用程序,如果我不在一个页面,否则关闭页面。

但是这不允许我关闭打开的模态。如何检测我是否处于关闭状态?

platform.registerBackButtonAction(() => { 

     let nav = app.getActiveNav(); 
     let activeView: ViewController = nav.getActive(); 
     console.log(activeView); 

     if(activeView != null){ 
     if(nav.canGoBack()) { 
      activeView.dismiss(); 
     } else{ 
      let alert = this.alertCtrl.create({ 
      title: this.pdataManager.translate.get("close-app"), 
      message: this.pdataManager.translate.get("sure-want-leave"), 
      buttons: [ 
       { 
       text: this.pdataManager.translate.get("no"), 
       handler:() => { 
        this.presentedAlert = false; 
       }, 
       role: 'cancel', 
       }, 
       { 
       text: this.pdataManager.translate.get("yes"), 
       handler:() => { 
        this.presentedAlert = false; 
        this.platform.exitApp(); 
       } 
       } 
      ] 
      }); 
      if(!this.presentedAlert) { 
      alert.present(); 
      this.presentedAlert = true; 
      } 
     } 
     } 
    }); 
    } 

回答

1

您可以为您的模式指定页面名称,并且您可以从应用程序的任何位置访问它。试试这个..

import { App } from 'ionic-angular'; 

    constructor(public app: App){ 

    } 

     platform.registerBackButtonAction(() => { 

       let nav = this.app.getActiveNav(); 
       let view = nav.getActive().instance.pageName; 


       if (view == YOU_PAGE_NAME) { 
       //You are in modal 
       } else { 
       //You are not in modal 
       } 
     }); 

内,您的情态

pageName = 'YOU_PAGE_NAME'; 
1

1,引进IonicApp:

import {IonicApp } from 'ionic-angular'; 

2.添加到您的构造函数:

private ionicApp: IonicApp 

3.Inside您platform.registerBackButtonAction地址:

let activeModal=this.ionicApp._modalPortal.getActive(); 
if(activeModal){ 
    activePortal.dismiss(); 
     return; 
    } 

我在这里找到答案: https://github.com/ionic-team/ionic/issues/6982