簡體   English   中英

.asObservable不想使用Observable.forkJoin

[英].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.forkJoinObservable變量config

我需要在服務中存儲配置並等待它們點亮它們並且其他請求沒有完成停止加載器。

由於您正在使用BehaviorSubject您應該知道可以手動調用next()complete()

forkJoin()運算符僅在其所有源Observable都發出至少一個值並且它們全部完成時才會發出。 由於您正在使用Subject和asObservable方法,因此源Observable永遠不會完成,因此forkJoin運算符永遠不會發出任何內容。

順便說一句,使用只有一個源Observable的forkJoin沒有多大意義。 也許可以看一下類似的zip()combineLatest()運算符,也許它就是你需要的。

兩個非常相似的問題:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM