简体   繁体   English

.asObservable不想使用Observable.forkJoin

[英].asObservable dont want to work with Observable.forkJoin

I have service: 我有服务:

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) );
  }
}

Trying to call this from component: 试图从组件中调用它:

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

someService.config.subscribe( res => console.log(res) ) // working
...

How can i use Observable.forkJoin with Observable variable config ? 如何将Observable.forkJoinObservable变量config

I need to store configs in service and wait unlit them and others request not finished to stop loader. 我需要在服务中存储配置并等待它们点亮它们并且其他请求没有完成停止加载器。

Since you're using BehaviorSubject you should know that you can call next() and complete() manually. 由于您正在使用BehaviorSubject您应该知道可以手动调用next()complete()

The forkJoin() operator emits only when all of its source Observables have emitted at least one values and they all completed. forkJoin()运算符仅在其所有源Observable都发出至少一个值并且它们全部完成时才会发出。 Since you're using a Subject and the asObservable method the source Observable never completes and thus the forkJoin operator never emits anything. 由于您正在使用Subject和asObservable方法,因此源Observable永远不会完成,因此forkJoin运算符永远不会发出任何内容。

Btw, it doesn't make much sense to use forkJoin with just one source Observable. 顺便说一句,使用只有一个源Observable的forkJoin没有多大意义。 Also maybe have a look at zip() or combineLatest() operators that are similar and maybe it's what you need. 也许可以看一下类似的zip()combineLatest()运算符,也许它就是你需要的。

Two very similar question: 两个非常相似的问题:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM