.asObservable不想使用Observable.forkJoin

Nya*_*aha 4 javascript fork-join observable rxjs angular

我有服务:

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) );
  }
}
Run Code Online (Sandbox Code Playgroud)

试图从组件中调用它:

...
Observable.forkJoin([someService.config])
  .subscribe( res => console.log(res) ) //not working 

someService.config.subscribe( res => console.log(res) ) // working
...
Run Code Online (Sandbox Code Playgroud)

如何使用Observable.forkJoinObservable变量config

我需要在服务中存储配置并等待它们点亮它们并且其他请求没有完成停止加载器.

mar*_*tin 5

因为你正在使用BehaviorSubject你应该知道你可以打电话next()complete()手动.

forkJoin()仅当所有源Observable都发出至少一个值并且它们全部完成时,运算符才会发出.由于您正在使用Subject和asObservable方法,因此源Observable永远不会完成,因此forkJoin操作符永远不会发出任何内容.

顺便说一下forkJoin,只使用一个源Observable 没有多大意义.也许可以看看zip()combineLatest()类似的运营商,也许这就是你需要的.

两个非常相似的问题: