【问题标题】:Ionic 2 redirect to another page after app boot应用程序启动后,Ionic 2 重定向到另一个页面
【发布时间】:2023-03-14 15:24:01
【问题描述】:

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

【问题讨论】:

  • 我想你也是在 constructor() 之前设置了 rootPage 吧?
  • 是的,我想要。但我从不介意怎么做

标签: angular typescript ionic2


【解决方案1】:

你喜欢吗

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

【讨论】:

  • Np :) 当我需要决定在开始时应该显示哪个页面时,我总是使用类似的方法。我认为这个答案应该是公认的答案。
  • 是的,正确的方法。欣赏。我将在项目中使用它。
  • @PareshGami 感谢您的解决方案。效果很好。
  • @sebaferreras 感谢您的编辑。这比创建一个新的启动页面要简单
【解决方案2】:

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

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

也许它可以帮助其他人

【讨论】:

    猜你喜欢
    • 2016-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2017-06-08
    • 2017-11-27
    • 1970-01-01
    • 2019-01-24
    相关资源
    最近更新 更多