How to handle back button on Ionic 2

前端 未结 12 1551
野的像风
野的像风 2020-11-29 04:04

How can I handle the back button action on Ionic 2?

I want to be able to know what to do depending on which page is being shown to the user.

I didn\'t find a

12条回答
  •  伪装坚强ぢ
    2020-11-29 04:47

    I was able to accomplish this in the event that we are simply setting root pages...

    import {Component, ViewChild, Injector} from '@angular/core';
    import {Platform, MenuController, Nav, App, IonicApp, NavController} from 'ionic-angular';
    import {StatusBar} from '@ionic-native/status-bar';
    import {SplashScreen} from '@ionic-native/splash-screen';
    import {InvitesPage} from "../pages/invites/invites";
    import {RewardsPage} from "../pages/rewards/rewards";
    import {ConnectionsPage} from "../pages/connections/connections";
    import {MessagesPage} from "../pages/messages/messages";
    import {ResourcesPage} from "../pages/resources/resources";
    import {SignoutPage} from "../pages/signout/signout";
    import {DashboardPage} from "../pages/dashboard/dashboard";
    import {AccountPage} from "../pages/account/account";
    import {HomePage} from "../pages/home/home";
    import {TriviaPage} from "../pages/trivia/trivia";
    import {Events} from "ionic-angular/util/events";
    
    
    @Component({
      templateUrl: 'app.html'
    })
    export class MyApp {
      @ViewChild(Nav) nav: NavController;
      // make HelloIonicPage the root (or first) page
    
      public rootPage: any; //if logged in, go to dashboard.
      public pages: Array<{title: string, component: any}>;
      public user: any;
      public routeHistory: Array;
    
      constructor(public platform: Platform,
                  public menu: MenuController,
                  public statusBar: StatusBar,
                  public splashScreen: SplashScreen,
                  private _app: App,
                  private _ionicApp: IonicApp,
                  private _menu: MenuController,
                  protected injector: Injector,
                  public _events: Events) {
    
        this.initializeApp();
    
        // set our app's pages
        this.pages = [
          {title: 'My Account', component: AccountPage},
          {title: 'Dashboard', component: DashboardPage},
          {title: 'Invites', component: InvitesPage},
          {title: 'Rewards', component: RewardsPage},
          {title: 'Connections', component: ConnectionsPage},
          {title: 'Messages', component: MessagesPage},
          {title: 'Resources', component: ResourcesPage},
          {title: 'Trivia', component: TriviaPage},
          {title: 'Sign Out', component: SignoutPage}
    
        ];
    
        this.routeHistory = [];
        this.user = {firstName: ''};
    
      }
    
      initializeApp() {
    
        this.platform.ready().then(() => {
    
          this._setupBrowserBackButtonBehavior();
    
          let self = this;
          if (sessionStorage.getItem('user')) {
            this.user = JSON.parse(sessionStorage.getItem('user'));
            self.rootPage = TriviaPage;
          } else {
            self.rootPage = HomePage;
          }
    
          this.routeHistory.push(self.rootPage);
          // Okay, so the platform is ready and our plugins are available.
          // Here you can do any higher level native things you might need.
          this.statusBar.styleDefault();
          this.splashScreen.hide();
        });
      }
    
      openPage(page) {
        // close the menu when clicking a link from the menu
        this.menu.close();
        // navigate to the new page if it is not the current page
        this.nav.setRoot(page.component);
        //store route history
        this.routeHistory.push(page.component);
      }
    
    
      private _setupBrowserBackButtonBehavior() {
    
        // Register browser back button action(s)
        window.onpopstate = (evt) => {
    
          // Close menu if open
          if (this._menu.isOpen()) {
            this._menu.close();
            return;
          }
    
          // Close any active modals or overlays
          let activePortal = this._ionicApp._loadingPortal.getActive() ||
            this._ionicApp._modalPortal.getActive() ||
            this._ionicApp._toastPortal.getActive() ||
            this._ionicApp._overlayPortal.getActive();
    
          if (activePortal) {
            activePortal.dismiss();
            return;
          }
    
          if (this.routeHistory.length > 1) {
            this.routeHistory.pop();
            this.nav.setRoot(this.routeHistory[this.routeHistory.length - 1]);
          }
    
    
        };
    
        // Fake browser history on each view enter
        this._app.viewDidEnter.subscribe((app) => {
          if (this.routeHistory.length > 1) {
            history.pushState(null, null, "");
          }
    
        });
    
      }
    }
    

提交回复
热议问题