首页 > 解决方案 > ionicV3 后退按钮导航问题显示 nav.getActive 不是函数

问题描述

我正在尝试使用 ionic 3 将导航返回按钮覆盖到我在 android 中的需要,但是每当我运行我的应用程序时,当我按下返回导航按钮说“nav.getActive不是一个功能”时,它会显示一个错误,因为这个返回按钮不起作用。

更新的代码如下

export class MyApp {

  rootPage:any = LoginPage;
  public counter=0;
  @ViewChild(Nav) nav: Nav;

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen,public globalProvider: GlobalProvider,
    public googlePlus: GooglePlus, public zone: NgZone, private readonly firebase: Firebase, private alertCtrl: AlertController, 
    public toastCtrl: ToastController, 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();
       platform.registerBackButtonAction(() => {


       const activeView = this.nav.getActive().name // This will give you the activeview name               

         if(activeView === 'HomePage') {

             if (this.nav.canGoBack()){ //Can we go back?
                 this.nav.pop();
             } else {
                 const alert = this.alertCtrl.create({
                     title: 'App termination',
                     message: 'Do you want to close the app?',
                     buttons: [{
                         text: 'Cancel',
                         role: 'cancel',
                         handler: () => {
                             console.log('Application exit prevented!');
                         }
                     },{
                         text: 'Close App',
                         handler: () => {
                             platform.exitApp(); // Close this application
                         }
                     }]
                 });
                 alert.present();
             }
         }
         this.nav.push(TabsPage);
     }, 1);

      this.firebase.onNotificationOpen().subscribe(notification => {
          console.log('notification info: ', notification);

          /*!notification.tap
              ? console.log('The user was using the app when the notification arrived...')
              : console.log('The app was closed when the notification arrived...');

          let notificationAlert = this.alertCtrl.create({
              title: notification.title,
              message: notification.body,
              buttons: ['Ok']
          });
          notificationAlert.present();*/
      },
      error => {
          console.error('Error getting the notification', error);
      });             
 });

       /*platform.registerBackButtonAction(() => {
        if (this.counter == 0) {
          this.counter++;
          this.presentToast();
          setTimeout(() => { this.counter = 0 }, 3000)
        } else {
          // console.log("exitapp");
          platform.exitApp();
        }
      }, 0) });*/
  }

标签: ionic-frameworkionic3navbar

解决方案


像这样试试。

    @ViewChild(Nav) nav: Nav;
    constructor(platform: Platform,
         statusBar: StatusBar,
         splashScreen: SplashScreen,
         public globalProvider: GlobalProvider, 
         public toastCtrl: ToastController,
         public googlePlus: GooglePlus,
         public zone: NgZone,
         public  app: App, 
         public alertCtrl: AlertController
    ) {
        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();
       platform.registerBackButtonAction(() => {

       **const activeView = this.nav.getActive().name** // This will give you the activeview name               

        if(active.instance instanceof HomePage) {

            if (nav.canGoBack()){ //Can we go back?
                nav.pop();
            } else {
                const alert = this.alertCtrl.create({
                    title: 'App termination',
                    message: 'Do you want to close the app?',
                    buttons: [{
                        text: 'Cancel',
                        role: 'cancel',
                        handler: () => {
                            console.log('Application exit prevented!');
                        }
                    },{
                        text: 'Close App',
                        handler: () => {
                            platform.exitApp(); // Close this application
                        }
                    }]
                });
                alert.present();
            }
        }
        nav.push(HomePage);
    }, 100);
});
}

推荐阅读