问题:
一旦用户成功登录,它就会导航到主页。同样,如果用户在 url 上尝试 /login,那么它会导航回登录页面。如果已经登录,那么它应该导航到主页而不是登录页面。我怎样才能做到这一点。
这是我的路线
const routes: Routes = [
{ path: '', redirectTo: 'home', pathMatch: 'full' },
{ path: 'home', component: HomeComponent, canActivate: [AuthGuard] },
{ path: 'login', component: LoginComponent },
{ path: '**', redirectTo: '' }
];
授权守卫服务。
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
const currentUser = this.authenticationService.currentUserValue;
console.log("current User 2:"+currentUser);
if (currentUser) {
// logged in so return true
return true;
}
// not logged in so redirect to login page with the return url
this.router.navigate(['/login'], { queryParams: { returnUrl: state.url } });
return false;
}
您可以像这样保护您的登录页面:
警卫:
constructor(private authenticationService: AuthenticationService){}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
return !this.authenticationService.isLoggedIn();
}
路由模块:
{ path: 'login', component: LoginComponent, canActivate: [LoginGuard] }
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句