2017-04-06 54 views
3

工作,我有服務:.asObservable不希望與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

我需要在服務中存儲配置,並等待未點亮它們和其他請求未完成停止加載程序。

回答

2

由於您使用的是BehaviorSubject您應該知道您可以手動撥打next()complete()

forkJoin()forkJoin()運營商只有當它的所有源觀測站發射至少一個值它們全部完成時才發射。由於您使用的是Subject和asObservable方法,所以源Observable永遠不會完成,因此運算符永遠不會發出任何東西。

順便說一句,使用forkJoin只有一個源Observable沒什麼意義。也可以看看zip()combineLatest()運營商是相似的,也許這是你所需要的。

兩個非常相似的問題:

+0

tywm的答案 – user2886051