2017-10-10 95 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