繁体   English   中英

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

[英]Ionic 2 redirect to another page after app boot

我想在localStorage上已经有令牌时将页面重定向到主页(覆盖登录页面)。 怎么做? 我在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;
          }
        })
      }

你能不能

    @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;
        });
     }

解决方案是生成一个名为splash的页面并将app的根页面设置为它,在构造函数中,我们检查令牌。 如果失败则将根页面设置为登录,如果成功将根页面设置为主页。

我认为这是唯一的解决方案。 启动页面作为凭据检查器。

也许它有助于其他人

暂无
暂无

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

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