简体   繁体   中英

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

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM