I'm trying to return an observable when I get a certain value in a subscriber, but I fail miserably.
This is the code:
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> {
// get route to be activated
this.routeToActivate = route.routeConfig.path;
// get user access levels
return this._firebase.isUserAdmin <-- returns Subscription, not Observable
.map(user => user.access_level)
.subscribe( access => {
// I need to return an observable here
});
}
There are not many resources on observables in angular 2, so I don't know where to start. Can anyone help with this please?
UPDATE -> Working Version
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> {
// get route to be activated
this.routeToActivate = route.routeConfig.path;
// get user access levels
return this._firebase.isUserAdmin
.map(user => {
let accessLevel = user.access_level;
if (accessLevel === 'admin' ) {
return true;
}
}).first();
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…