这是我的角度项目的路由器守卫 . 基本上在我的系统中我有很多用户角色 . 在guard中,如果用户isLawyer和isLawyerApproved,请检查userProfile节点 . 如果这些条件成立,律师可以访问该路线 . 在canActivate函数中,我返回this.loggedin && this.isApproved . 在我的登录组件中,我有一个ngOnit函数,用于检查用户是否已经登录,如果是,则重定向到/ dashboard路由 . 实际上,这不起作用,我希望如果经过身份验证的用户访问登录页面,则会自动重定向到/仪表板 . 请帮我
class MyGuardService implements CanActivate{
loggedIn = false;
isApproved:boolean;
constructor(private authService:AuthService, private router:Router){
this.authService.isLogged().subscribe((response)=>{
if(response && response.uid){
this.loggedIn = true;
this.authService.getUserProfile(response.uid).valueChanges()
.subscribe(
(userProfile:UserProfile)=>{
if(userProfile.isLawyer && userProfile.isLawyerApproved){
this.isApproved = true;
}
}
)
}else{
this.loggedIn = false
}
}, (error)=>{
this.loggedIn = false;
})
}
canActivate(route:ActivatedRouteSnapshot,
state:RouterStateSnapshot):Observable<boolean>|Promise<boolean>|boolean{
console.log('El abogado es aprobado??')
console.log(this.isApproved)
if(!this.loggedIn){
this.router.navigate(['/abogado-login']);
}
return (this.loggedIn && this.isApproved)
}}
// LoginComponent
ngOnInit() {
this.authService.isLogged().subscribe((result)=>{
if(result && result.uid){
console.log('Estoy logueado debo redirigir');
this.router.navigate(['/abogado'])
}
})
}
1 回答
在您的gaurd中,只需通过在登录true成功时强制路由器添加重定向,或者在路由器模块中登录路由上的条件和地点 .
在路由器模块中,在下面类似的路径路径上添加gaurd .