Ionic2 具有多个根组件的Ionic3路由
我有一个Ionic应用程序,有些页面需要登录,有些页面不需要。当用户登录时,我想显示菜单,当未登录时,用户应该看到没有菜单的页面。为此,我们使用两个根组件:SecurePage和PublicPage。SecurePage有菜单,PublicPage没有。ProductList页面需要登录,并且需要在SecurePage内呈现。在app.ts中,我们根据用户是否拥有访问令牌设置根页面:Ionic2 具有多个根组件的Ionic3路由,ionic2,ionic3,Ionic2,Ionic3,我有一个Ionic应用程序,有些页面需要登录,有些页面不需要。当用户登录时,我想显示菜单,当未登录时,用户应该看到没有菜单的页面。为此,我们使用两个根组件:SecurePage和PublicPage。SecurePage有菜单,PublicPage没有。ProductList页面需要登录,并且需要在SecurePage内呈现。在app.ts中,我们根据用户是否拥有访问令牌设置根页面: ngOnInit() { this.translateService.addLangs(["en",
ngOnInit() {
this.translateService.addLangs(["en", "nl"]);
this.translateService.setDefaultLang('en');
this.authService.getAccessToken()
.flatMap((access_token) => {
if (access_token)
return this.springApiService.isTokenValid(access_token);
else
return Observable.of(false)
})
.subscribe(isTokenValid => {
if (isTokenValid)
this.rootPage = SecurePage;
else
this.rootPage = PublicPage;
});
}
然后在SecurePage.ts
中有:
ionViewCanEnter(): Promise<boolean> {
return this.authService.getPrincipal()
.map(principal => {
this.principal = principal;
this.hasRoleSuperuser = this.authService.hasRoleSuperuser;
this.hasRoleSales = this.authService.hasRoleSales;
this.hasRoleAccountant = this.authService.hasRoleAccountant;
this.hasRoleManager = this.authService.hasRoleManager;
this.menuCtrl.enable(true);
this.navCtrl.popAll()
.then(() => this.navCtrl.setRoot('TodoListComponent'));
return true;
})
.catch(error => {
return Observable.of(false);
}).toPromise();
}
当从菜单中导航时,一切正常。但是,如果在查看产品列表时刷新页面,则行为是不可预测的。有时它会再次发送到产品列表,这正是我想要的。但大多数情况下,安全页面都是在产品列表之后加载的,因此
this.navCtrl.popAll()
.then(() => this.navCtrl.setRoot('TodoListComponent'));
该行位于最后,所以它路由到待办事项列表,而不是产品列表。如何确保路由在设置根页面后生效?或者是否有其他方法可以实现多母版页逻辑,并且路由工作正常
this.navCtrl.popAll()
.then(() => this.navCtrl.setRoot('TodoListComponent'));