Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ??

like image 946
zahirnet Avatar asked Jan 05 '23 20:01

zahirnet


1 Answers

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 Observable.of(true);
}
like image 87
Günter Zöchbauer Avatar answered Jan 18 '23 13:01

Günter Zöchbauer