Angular 2 - Routing - CanActivate work with Observable

前端 未结 7 1016
终归单人心
终归单人心 2020-11-30 18:30

I have an AuthGuard (used for routing) that implements CanActivate.

canActivate() {
    return this.loginService.isLoggedIn         


        
相关标签:
7条回答
  • 2020-11-30 18:57

    canActivate() accepts Observable<boolean> as returned value. The guard will wait for the Observable to resolve and look at the value. If 'true' it will pass the check, else ( any other data or thrown error ) will reject the route.

    You can use the .map operator to transform the Observable<Response> to Observable<boolean> like so:

    canActivate(){
        return this.http.login().map((res: Response)=>{
           if ( res.status === 200 ) return true;
           return false;
        });
    }
    
    0 讨论(0)
提交回复
热议问题