Angularjs   发布时间:2022-04-20  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了angular – 在’authGuard’中的canActivate无法读取AuthServer的’可观察的当前用户’大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用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)
    })
  }

大佬总结

以上是大佬教程为你收集整理的angular – 在’authGuard’中的canActivate无法读取AuthServer的’可观察的当前用户’全部内容,希望文章能够帮你解决angular – 在’authGuard’中的canActivate无法读取AuthServer的’可观察的当前用户’所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。