Angular 2 - service call inside canActivate

angular

Solution

You just need to change the signature of canActivate

@Injectable()
export class AuthGuardService implements CanActivate {
constructor(private authService: AuthService) {}

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
    let url: string = state.url;

    this.authService.canAccessUrl(url)
    .then( (answer:boolean) => {return answer;} );

   }
}

Please check https://angular.io/docs/ts/latest/api/router/index/CanActivate-interface.html

Problem

Using guards, I try to access a service but I cant return a promise in canActivate (which has particular signature that I cannot change) my autService returns a promise since it is asynchrone how can I achieve something similar : ``` @Injectable() export class AuthGuardService implements CanActivate { constructor(private authService: AuthService) {} canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { let url: string = state.url; this.authService.canAccessUrl(url) .then( (answer:boolean) => {return answer;} ); } } ``` thanks

Original source