我的主要URL是example.com
,登录后将是example.com/homee
。登录后,用户不得看到主URL,并且如果有人进入主URL,则必须将其重定向到example.com/homee
,而当用户注销时,用户将看不到example.com/homee
,并且必须重定向到主URL 。
为此,我编写了2个警卫,example.com
有1个警卫,example.com/homee
有1个警卫,如下所示:
在应用程序路由模块中。
{path: '', component: HomeBlComponent, canActivate: [LoggedInUserGuard]},
{path: 'homee', component: AppHomeComponent, canActivate: [AuthGuard]},
以及auth-gurd.services.ts
@Injectable()
export class LoggedInUserGuard implements CanActivate {
constructor(private router: Router, private authService: AuthService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (this.authService.isAuthenticated()) {
this.router.navigate(['/homee']);
return this.authService.isAuthenticated();
}
}
}
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router, private authService: AuthService) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
if (!this.authService.isAuthenticated()) {
this.router.navigate(['/']);
return this.authService.isAuthenticated();
}
return this.authService.isAuthenticated();
}
}
这里的问题是,当isAuthenticated
为 false 时(意味着我们没有登录网站),该网站的页眉和页脚之间的内容被隐藏了,看来我的homeBeforLogin
组件无法运行。
看下面的照片。第二个是主要站点,我们必须看到它,但是当我应用这些警卫时,我们看到了第一个图片。而且我认为问题出在我的LoggedInUserGuard
上,但我不知道解决方案。