typescript 将布尔值从 observable 传递到 observable
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40835065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass boolean from observable to observable
提问by Harald Wiesinger
I want to check in my adminauthguard if the user has set a adminflag(in Firebase using angularfire2)
如果用户设置了 adminflag(在 Firebase 中使用 angularfire2),我想检查我的 adminauthguard
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> | boolean {
return this.loginservice.af.auth.map((auth) => {
if(auth == null) {
return false;
} else {
this.membersservice.get(auth.uid).subscribe(users => {
if(users.admin == true) {
return true;
} else {
return false;
}
})
}
});
how can I resolve the observable in the observable inside?
我如何解决里面的 observable 中的 observable?
回答by lastWhisper
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):Observable<boolean> | boolean {
return new Observable<boolean>( observer => {
this.loginservice.af.auth.map((auth) => {
if(auth == null) {
observer.next(false);
} else {
this.membersservice.get(auth.uid).subscribe(users => {
if(users.admin == true) {
observer.next(true);
} else {
observer.next(false);
}
})
}
});
Not 1000% sure if this will work with your service Observables, since I don't know Firebase for Angular2 exactly (only iOS), but this way you create an observable that just emits values based on the events that are happening inside the closure.
不能 1000% 确定这是否适用于您的服务 Observables,因为我不完全了解 Angular2 的 Firebase(仅适用于 iOS),但是通过这种方式,您可以创建一个仅根据闭包内发生的事件发出值的 observable。
The only thing you may need to make sure (with tests) is that you do not run into undefined states, e.g. something like async hazards where you both emit true and false.
您可能需要确保(通过测试)的唯一一件事是您不会遇到未定义的状态,例如像异步危险之类的东西,您同时发出真和假。
回答by Manuel Villena
I need wait User Suscription info before to run any secure component. I use a similar solution to lastWhisper's, implementing canActivateChild and setting in routes:
在运行任何安全组件之前,我需要等待用户订阅信息。我使用与 lastWhisper 类似的解决方案,实现 canActivateChild 并在路由中设置:
path: 'app', component: FullLayoutComponent, data: {title: 'Home'}, canActivateChild: [CanActivateChildTeam],
CanActivateChildTeam Service:
import {Injectable, OnDestroy} from "@angular/core";
import {CanActivateChild} from "@angular/router";
import {Meteor} from 'meteor/meteor';
import {Router} from '@angular/router';
import {Observable} from "rxjs/Rx";
import {MeteorObservable} from 'meteor-rxjs';
import {Subscription} from 'rxjs/Subscription';
@Injectable()
export class CanActivateChildTeam implements CanActivateChild, OnDestroy {
allow:Observable<boolean>;
private _userSub:Subscription;
constructor(private router:Router) {
}
canActivateChild(route:any, state:any):Observable<boolean> | Promise<boolean> | boolean {
return this.getUser();
}
getUser() {
if (this.allow) {
this.allow = new Observable(observer => {
observer.next(true);
observer.complete();
});
return this.allow;
}
if (Meteor.userId()) {
this.allow = new Observable(observer => {
if (this._userSub)
this._userSub.unsubscribe();
this._userSub = MeteorObservable.subscribe('user', Meteor.userId()).subscribe(
() => {
observer.next(true);
observer.complete();
}
);
});
return this.allow;
}
else {
this.router.navigate(['/']);
}
}
ngOnDestroy():void {
if (this._userSub)
this._userSub.unsubscribe();
}
}