zahirnet zahirnet - 2 months ago 25
TypeScript Question

angular2 canActivate() not work with Observable response

i have problem with canActivate angular2.0.0-rc.3.

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>{
console.log('canActivate with AclService');
// return true;
return Observable.create((observer:Subject<boolean>) => { observer.next(true); });
}


it's not work with Observable response but work with simple boolean response.

how can i fix this problem ??

Answer

If you add .first() or complete the Observable by other means it should work:

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>{
    console.log('canActivate with AclService'); 
   // return true;
    return Observable.create((observer:Subject<boolean>) => { observer.next(true);  }).first();
}

or

canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean>{
    console.log('canActivate with AclService'); 
   // return true;
    return return Observable.of(true);
}