Angular 2 - Routing - CanActivate work with Observable

前端 未结 7 1053
终归单人心
终归单人心 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 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 to Observable like so:

    canActivate(){
        return this.http.login().map((res: Response)=>{
           if ( res.status === 200 ) return true;
           return false;
        });
    }
    

提交回复
热议问题