2017-02-03 50 views
0

我試圖處理我的Android早在下方離子2鍵是我的代碼,這將讓用戶退出應用程序時,他們從主頁或登錄頁面點擊離子2:處理機器人後退按鈕

let alert = this.alertCtrl.create({ 
      title: 'Kmart', 
      message: 'Do you want exit kmart app ?', 
      buttons: [ 
       { 
       text: 'Cancel', 
       role: 'cancel', 
       handler:() => { 
        this.logger.info("cancel clicked") 
       } 
       }, 
       { 
       text: 'ok', 
       handler:() => { 
        this.platform.exitApp(); 
       } 
       } 
      ] 
      }); 

     this.platform.registerBackButtonAction(() => { 
      console.log('back button pressed') 
      if (this.navCtrl.canGoBack()) { 
      console.log('nav can go back') 
      this.navCtrl.pop() 
      } else { 
      //here i have to show the alert for login and home page 
      alert.present(); 
      } 
     }, 100); 

上面這段代碼放在我的home.ts文件中

我無法獲取當前頁面名稱,因此我可以讓我的應用程序退出。

當用戶在家中或登錄屏幕上雙擊應用程序時,如何退出應用程序。

回答

0

使用

getActive()返回當前組件檢查實例的的ViewController。 使用ViewChild得到navController在app.component.ts 設置#mynav模板

模板:<ion-nav #myNav [root]="rootPage"></ion-nav>

在組件類:

@ViewChild('myNav') navCtrl: NavController 
//to check the condition 
let view = this.navCtrl.getActive(); 
if(view instanceof HomePage) 

檢查ionic forum discussion

+0

'例外:不提供for t!'這個錯誤我正面臨着 –

+0

getActive返回一個viewController ..也許嘗試在構造函數中注入viewcontroller .. –

+0

或它可能是一個無關的問題... –