Want to return observable from subscribe? You can’t return an observable from subscribe but if you use map instead of subscribe then an Observable is returned.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> { // get route to be activated this.routeToActivate = route.routeConfig.path; // get user access levels return this._firebase.isUserAdminObservable .map(user => { // do something here // user.access_level; return true; }) .first(); // for the observable to complete on the first event (usually required for `canActivate`) // first needs to be imported like `map`, ... } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> { // get route to be activated this.routeToActivate = route.routeConfig.path; let subject = new Subject(); // get user access levels this._firebase.isUserAdminObservable .map(user => { let accessLevel = user.access_level; if (accessLevel === 'admin' ) { subject.emit(true); subject.complete(); } return user; }); return subject; } |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.