当本地存储上已有 token 时,我想将页面重定向到主页(覆盖登录页面)。怎么做?我在 app.component.ts 的 constructor() 上有以下代码,但它在请求完成之前首先显示登录
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;
});
}
关于angular - Ionic 2 在应用程序启动后重定向到另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44253750/