Angular 5 canActivate始终返回特定路线的登录页面

Angular 5 canActivate始终返回特定路线的登录页面,angular,canactivate,Angular,Canactivate,我正试图通过定义路由保护并为每个想要安全的路由分配canActivate来保护我的应用程序不被未经授权的用户使用。在这些路由上一切都进行得很顺利,例如,当有人想从url访问路由时,它将自动重定向到登录页面,除非他/她已登录,否则应用程序将重定向到特定的路由。但有一个路径,当用户登录时,它总是返回到登录页面。此问题发生在路由“/verify/:idC”,我该怎么办 路由。ts: {path:'verify/:idC',component:VerifyRoleComponent,canActivat

我正试图通过定义路由保护并为每个想要安全的路由分配canActivate来保护我的应用程序不被未经授权的用户使用。在这些路由上一切都进行得很顺利,例如,当有人想从url访问路由时,它将自动重定向到登录页面,除非他/她已登录,否则应用程序将重定向到特定的路由。但有一个路径,当用户登录时,它总是返回到登录页面。此问题发生在路由
“/verify/:idC”
,我该怎么办

路由。ts:

{path:'verify/:idC',component:VerifyRoleComponent,canActivate:[AuthGuard]},
auth-guard.ts:

从'@angular/core'导入{Injectable};
从'@angular/Router'导入{CanActivate,Router,ActivatedRouteSnapshot,RouterStateSnashot};
从“../auth/auth.service”导入{AuthService};
@可注射()
导出类AuthGuard实现了CanActivate{
构造函数(私有authService:authService,私有路由器:路由器){}
canActivate(路由:ActivatedRouteSnapshot,状态:RouterStateSnashot):布尔值{
让url:string=state.url;
返回这个.checkLogin(url);
}
checkLogin(url:string):布尔值{
if(this.authService.isLoggedIn){return true;}
//存储尝试重定向的URL
this.authService.redirectUrl=url;
//导航到带有附加功能的登录页面
this.router.navigate(['/login']);
返回false;
}
}
auth.service.ts:

@Injectable()
导出类身份验证服务{
isLoggedIn=false;
构造函数(){}
//存储URL,以便我们可以在登录后重定向
重定向URL:字符串;
login():可观察{
返回可观测的.of(true).delay(1000).do(val=>this.isLoggedIn=true);
}
注销():void{
this.isLoggedIn=false;
}
}
验证-component.ts:

@组件({
选择器:“应用验证角色”,
templateUrl:“./verify role.component.html”,
样式URL:['./验证role.component.scss']
})
导出类VerifyRoleComponent实现OnInit{
auth:auth;
构造函数(私有credService:CredentialService,私有authService:authService,
专用路由:激活的路由,
专用路由器:路由器{}
ngOnInit(){
const id=this.route.snapshot.paramMap.get('idC');
console.log(id);
此.route.paramMap
.switchMap((参数:ParamMap)=>
this.credService.getAccountId(params.get('idC'))
.订阅(res=>{
this.auth=res;
console.log(this.auth);
if(res.role.toString()=“用户”){
让idC=res.account;
这是登录用户(idC);
}else{}
},err=>{
控制台日志(err);
});
}
登录用户(idC:string){
this.authService.login().subscribe(()=>{
if(this.authService.isLoggedIn){
让redirect=this.authService.redirectUrl?this.authService.redirectUrl:'/profile/'+idC;
这个.router.navigate([redirect]);
}
});
}
}

我认为您必须在
这个.router.navigate(['/login'])之前添加一个else

我现在就试过了。没有发生任何事情,问题仍然存在。在this.authService.isLoggedIn条件下,您应该导航到重定向URL。所以添加这个.router.navigate([redirectUrl]);同样的,@user3529601由于登录用户方法条件失败,我得到了它,那么我如何解决这个问题呢?