[英].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.