简体   繁体   中英

Ionic 2 redirect to another page after app boot

I want to redirect page to home page (override login page) when it already has token on localStorage. How to do it? I have following code on constructor() at app.component.ts, but it display login first before request completed

statusBar.backgroundColorByHexString('#D32F2F');
      splashScreen.hide();
      if(localStorage.getItem('token')){
        authProvider.silent_login().subscribe(res => {
          console.log(res);
          if(res.error==0){
            this.rootPage = HomePage;
          }
        })
      }

Can you like

    @ViewChild(Nav) nav: Nav;
    rootPage: any = null; // Initialize it as null
    pages: Array<{title: string, component: any}>;

    constructor(public platform: Platform, 
                public statusBar: StatusBar, 
                public splashScreen: SplashScreen,
                public commonProvider: CommonProvider) {

        this.commonProvider.retrieve("is_login").then(loggedIn => {
            // Assign the right page after checking the status
            this.rootPage = loggedIn ? TabsPage : SigninPage;
        });
     }

The solution is generate one page named splash and set root page of app to it and at constructor, we check the token. If fails set root page to login and if success set root page to homepage.

I think thats the only solution. Splash page as credentials checker.

Maybe it helps other

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM