gpt4 book ai didi

angular - 当我使用 canActivate Guard 时,它会导致 Angular 4 中的空白页?

转载 作者:行者123 更新时间:2023-12-04 01:57:03 25 4
gpt4 key购买 nike

我正在尝试使用 Angular 4 实现登录功能,但是当我使用 canActivate Guard 时,它会将我带到一个空白页面,请帮助我

这是我的登录服务

 isAuthenticated(){
return localStorage.getItem('token') != null ;
}

这是可以激活守卫
  canActivate(route : ActivatedRouteSnapshot , state : RouterStateSnapshot){

return this.loginservice.isAuthenticated();
}

这是路由器模块
 const appRoutes: Routes = [
{path : '' , redirectTo:'/login', pathMatch :'full' },
{path : 'login' , component : LoginComponent },
{path : 'home' , component : HomeComponent, canActivate : [AuthGuard] ,children :[

{path : 'adduser' , component : AdduserComponent },
]

最佳答案

如果您未通过身份验证或未经授权,您的 AuthGuard 应该将您重定向到默认页面。

constructor( private loginservice: LoginService, private router: Router) {

}
public canActivate(route : ActivatedRouteSnapshot , state : RouterStateSnapshot){

if(!this.loginservice.isAuthenticated()) {
this.router.navigate(['login']); // choose either default route like here
return false;
}
return true;
}

关于angular - 当我使用 canActivate Guard 时,它会导致 Angular 4 中的空白页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49809971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com