Angular 2+ wait for method / observable to complete

前端 未结 2 1896
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 10:10

I need to check te back-end for authentication status, however te code completes before te observable return is finished. Which would result in an undifined.



        
2条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-13 10:42

    You can return the observable as Observable

    canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) {
        return this.loginService.isAuthenticated()
            .map(status => {
                console.log(status);
                this.authenticated = status;
                return true;
            }).catch((err) => {
                console.log(err);
                return Observable.of(false);
            });
    }
    

提交回复
热议问题