我有一个 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;
});
});
}
}
在这种情况下,如果用户已经登录,则根页面将是主页,如果未登录,则为登录页面。
关于angular - Ionic 2 auth - 如果本地存储上有用户,则跳过登录页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43377594/