4

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

zahirnet
  • 63
  • 1
  • 5
  • What does "it's not working" mean? What do you expect? What is happening? – Günter Zöchbauer Jun 26 '16 at 09:45
  • not working mean canActivate role is not working : if i return boolean ( true or false ) it's work mean if true aceess to current route is permit or not if return false , but if return Observable it's not work – zahirnet Jun 26 '16 at 09:53
  • I'm having the same issue. I followed answer from question http://stackoverflow.com/questions/37897273/authguard-doesnt-wait-for-authentication-to-finish-before-checking-user but It doesn't work. When I call `return true;` immediately, it works. – Darwin Gautalius Jun 26 '16 at 13:59
  • 1
    @DarwinGautalius i add the same issue to github angular project https://github.com/angular/angular/issues/9613 – zahirnet Jun 26 '16 at 22:50

1 Answers1

9

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);
}
cgatian
  • 22,047
  • 9
  • 56
  • 76
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567