[英].asObservable dont want to work with Observable.forkJoin
我有服务:
export class ConfigService {
private _config: BehaviorSubject<object> = new BehaviorSubject(null);
public config: Observable<object> = this._config.asObservable();
constructor(private api: APIService) {
this.loadConfigs();
}
loadConfigs() {
this.api.get('/configs').subscribe( res => this._config.next(res) );
}
}
试图从组件中调用它:
...
Observable.forkJoin([someService.config])
.subscribe( res => console.log(res) ) //not working
someService.config.subscribe( res => console.log(res) ) // working
...
如何将Observable.forkJoin
与Observable
变量config
?
我需要在服务中存储配置并等待它们点亮它们并且其他请求没有完成停止加载器。
由于您正在使用BehaviorSubject
您应该知道可以手动调用next()
和complete()
。
forkJoin()
运算符仅在其所有源Observable都发出至少一个值并且它们全部完成时才会发出。 由于您正在使用Subject和asObservable
方法,因此源Observable永远不会完成,因此forkJoin
运算符永远不会发出任何内容。
顺便说一句,使用只有一个源Observable的forkJoin
没有多大意义。 也许可以看一下类似的zip()
或combineLatest()
运算符,也许它就是你需要的。
两个非常相似的问题:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.