我想通过在成功登录或注册后手动在地址栏中输入网址来阻止用户导航到登录和注册网址。我为其他网址实现了Auth Guard,用户无法登录而无法访问。这是我的Auth Guard实现:
@Injectable()
export class AuthGuard implements CanActivate {
constructor(private router: Router,
private userService: UserService) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> | boolean {
if (!this.userService.getLoggedInStatus()) {
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
return true;
}
}
我正在我的app.route.ts中使用它:
{ path:'checkout', component:CheckoutComponent,canActivate:[AuthGuard] }
如何实现登录和注册组件的反向,即只有在用户未登录时才激活它们,并在用户登录后停用?