Angular authguard在使用路由器导航时不工作,可用于页面刷新

Angular authguard在使用路由器导航时不工作,可用于页面刷新,angular,Angular,因此,当没有会话时,我使用一个简单的authguard将我重定向到登录页面,但它只在我刷新页面时起作用 我的路由模块: const routes:routes=[ {路径:'',组件:HomeComponent,canActivate:[AuthGuardService]}, {path:'login',component:LoginComponent}, {路径:'logout',组件:LogoutComponent,canActivate:[AuthGuardService]} ]; 我的

因此,当没有会话时,我使用一个简单的authguard将我重定向到登录页面,但它只在我刷新页面时起作用

我的路由模块:

const routes:routes=[
{路径:'',组件:HomeComponent,canActivate:[AuthGuardService]},
{path:'login',component:LoginComponent},
{路径:'logout',组件:LogoutComponent,canActivate:[AuthGuardService]}
];
我的卫士:

导出类AuthGuardService实现CanActivate{
构造函数(专用路由器:路由器、,
私有authService:AuthenticationService{}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot){
if(this.authService.isUserLoggedIn())
返回true;
this.router.navigate(['login']);
返回false;
}
}

控制台中是否有错误?您的
this.authService.isUserLoggedIn()
实际上是做什么的?当然,如果页面中有http请求,我会出错!但是与guard无关,@AjmalAli isUserLoggedIn()检查会话中是否存储有用户名,放置该会话的服务的构造函数向后端发送请求,并检查是否仍有会话。如果没有,则清除会话管理!我认为,您可能还希望实现canActivateChild接口。请参见中解释的差异。HTHi相信当您有/book/:id、/book/:id/details等子路由时会使用此选项。。。但这不是我的情况