angular – 在’authGuard’中的canActivate无法读取AuthServer
发布时间:2020-12-17 17:16:39 所属栏目:安全 来源:网络整理
导读:我使用angular2 rc.3和@ angular / router“:”3.0.0-alpha.7 我的auth.server export class AuthService { isLogged:boolean; constructor(private principal:PrincipalService){ this.authenticated();} authenticated() { this.principal.currentUser().
我使用angular2 rc.3和@ angular / router“:”3.0.0-alpha.7
我的auth.server export class AuthService { isLogged:boolean; constructor(private principal:PrincipalService){ this.authenticated(); } authenticated() { this.principal.currentUser().subscribe( e=> { if (!!e && e.authenticated) { this.isLogged = true; } this.isLogged = false; },error=> { this.isLogged = false; } ) } AuthGuard: constructor(private auth:AuthService ) {} canActivate(next: ActivatedRouteSnapshot,state: RouterStateSnapshot): boolean{ // here the **auth.isLogged** is undefined !! why?** if(this.auth.isLogged){ return true; } else { window.location.href='/login'; return false; } 我设置了路由器 { path: 'property',component: PropertyHomeComponent,canActivate: [AuthGuard] } 当我打开’/ property’页面时,AuthGuard无法读取“this.auth.isLogged”,也没有重定向到’/ login’页面, 怎么解决? 解决方法
我找到了革命. (感谢@Michael的提示)
AuthGuard return this.principal.currentUser().map(e=> { if (e) { return true; } }).catch(()=> { window.location.href = ConstantService.loginUrl; return Observable.of(false) }) } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |