Ionic 2 auth - 如果本地存储上有用户,则跳过登录页面

Ionic 2 auth - Skip login page if there's an user on local storage

我有一个 Ionic 2 应用程序,它在我的 Rails 5 后端进行身份验证。用户登录后,我将他的信息存储在本地存储中。 因此,一旦用户打开应用程序并且他之前已经登录过,就应该跳过登录页面。我正在尝试在我的 app.component 上设置我的根页面,具体取决于本地存储中是否有关于用户的信息,但是 storage.get 方法似乎是异步的,所以它在之后执行我的支票,所以它总是认为它是假的。

有什么办法可以解决这个问题吗?

您可以在从存储中获取值后设置根页面,如下所示:

@Component({
    templateUrl: 'app.html'
})
export class MyApp {
    @ViewChild(Nav) navCtrl: Nav;

    public rootPage; // Just declare the property, don't set a value here

    constructor(...) {
      this.platform.ready().then(() => {
        Splashscreen.hide();

        // Get the status from the storage
        this.storage.get('login:status').then(loggedIn => {
          this.rootPage = loggedIn ? HomePage : LoginPage;
        });
      });
    }

}

在这种情况下,如果用户已经登录,则根页面将是主页,如果未登录,则为登录页面。