2017-02-11 5 views
1

私のルートページでのみ関数を呼び出す必要があります。これは、すべてのページのイオン2. 上のコールのコードは以下の通りですます:私のルートページでのみ関数を呼び出す必要があります。しかし、これはすべてのページでイオンを呼び出す2

constructor(public platform: Platform,public alertCtrl:AlertController) { 

     platform.ready().then(() => { 
      platform.registerBackButtonAction(()=>this.myHandlerFunction()) 
      StatusBar.styleDefault(); 
      Splashscreen.hide(); 

     }); 
    } 


    myHandlerFunction(){ 
     let alert = this.alertCtrl.create({ 
     title: 'Exit?', 
     message: 'Do you want to exit the app?', 
     buttons: [ 
      { 
      text: 'Cancel', 
      role: 'cancel', 
      handler:() => { 

      } 
      }, 
      { 
      text: 'Exit', 
      handler:() => { 
       this.platform.exitApp(); 
      } 
      } 
     ] 
     }); 
     alert.present(); 

    } 

私はmyHandlerFunction()あなたはapp.component.tsに次のように使用できる唯一の

+0

質問タイトルは「ハードウェアのバックボタンをクリックするとルートページにポップアップが表示される」 –

答えて

0

Homepageに呼び出される必要があります。

import { Component } from '@angular/core'; 
import { Platform,App, AlertController } from 'ionic-angular'; 
import { StatusBar, Splashscreen } from 'ionic-native'; 

import {LognPage} from '../pages/logn/logn'; 

@Component({ 
    template: `<ion-nav [root]="rootPage"></ion-nav>` 
}) 
export class MyApp { 
    rootPage = LognPage; 

    constructor(public platform: Platform, 
       private alertCtrl: AlertController, 
       public app: App) { 
    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(); 

     //Registration of push in Android and Windows Phone 
     platform.registerBackButtonAction(() => { 
     let nav = this.app.getActiveNav(); 
     if (nav.canGoBack()){ //Can we go back? 
      nav.pop(); 
     }else{ 
      this.myHandlerFunction(); 
     } 
     }); 
    }); 
    } 

    myHandlerFunction(){ 
     let alert = this.alertCtrl.create({ 
     title: 'Exit?', 
     message: 'Do you want to exit the app?', 
     buttons: [ 
     { 
      text: 'Cancel', 
      role: 'cancel', 
      handler:() => { 

      } 
     }, 
     { 
      text: 'Exit', 
      handler:() => { 
      this.platform.exitApp(); 
      } 
     } 
     ] 
    }); 
    alert.present(); 
    } 
} 

Tookこのスレッドからの参照。

ハッピーコーディング!

+0

ありがとう!私のために働いた! –

関連する問題