return boolean instead of subscribe to canActivate

后端 未结 1 1933
遥遥无期
遥遥无期 2020-12-19 11:03

I have a component protected with canActivate guard. The Authguard in the checkLogin function subscribes from an observable but I do not know how to return a bo

相关标签:
1条回答
  • 2020-12-19 11:31

    canActivate can return an observable too, see the definition CanActivate-interface. Just change your return types accordingly.

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {
        let url: string = state.url;
    
        return this.checkLogin(url);
    }
    
    public checkService:boolean;
    checkLogin(url: string):Observable<boolean> {
         return this.loadAppSettings().map(res=>{
              console.log(this.checkservice);
              if (this.checkservice == true) 
              {
                   return true; 
            }
            else{
                this.router.navigate(['/error-user']);
                return false;
            }
        });
      }
    
    0 讨论(0)
提交回复
热议问题