我有一个角度为2的主组件和导航面板组件 . 当用户单击导航面板(侧栏)上的注销时,通过将路由器记录到控制台,将当前URL正确报告为“/ login” . 当用户再次登录时,导航面板中的路由器实例将当前路由报告为“/ login”,当用户登录后,路由器应将其带到主页 . 因此,用户永远无法通过登录屏幕,并且浏览器中的视图和URL不会更新 .
到目前为止,我尝试了以下内容:
-
手动清除浏览器缓存并刷新页面(这似乎解决了问题)
-
在对ngZone.run()的调用中包装router.navigate调用
-
清除Angular 2 's template cache in the home component'的构造函数 .
以下是代码的一些相关部分:用户登录后处理导航的方法(在login.component.ts中)
private doAfterLoggedIn() {
let returnRoute = this.route.snapshot.queryParams["ReturnUrl"] || "/";
this.toaster.toast({
title: `Welcome back!`,
duration: 2000
});
this.router.navigate([returnRoute]);
}
Home组件的路由:
{
path: '',
pathMatch: 'full',
component: HomeComponent,
resolve: { author: RouteAuthorResolver },
data: {
panelsectionName: "home"
}
},
导航面板组件中的部分退出方法:
doLogout() {
// leave immediately
this.router.navigate(["/login"]);
}
1 回答
假设你是bootstrapping组件是
HomeComponent
所以在它的构造函数中检查登录else重定向到登录组件 .