简体   繁体   English

应用程序启动后,Ionic 2重定向到另一个页面

[英]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. 我想在localStorage上已经有令牌时将页面重定向到主页(覆盖登录页面)。 How to do it? 怎么做? I have following code on constructor() at app.component.ts, but it display login first before request completed 我在app.component.ts上的构造函数()上有以下代码,但它在请求完成之前首先显示登录

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. 解决方案是生成一个名为splash的页面并将app的根页面设置为它,在构造函数中,我们检查令牌。 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 也许它有助于其他人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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