本文介绍了Observable forkJoin 不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在两个 Observable 上使用 forkJoin
.其中一个以流的形式开始...如果我直接订阅它们,我会收到回复,但 forkJoin
不会触发.有什么想法吗?
I'm trying to use forkJoin
on two Observables. One of them starts as a stream... If I subscribe to them directly I get a response, forkJoin
isn't firing though. Any ideas?
private data$: Observable<any[]>;
private statuses$: Observable<any[]>;
private queryStream = new Subject<string>();
....
this.data$ = this.queryStream
.startWith('')
.flatMap(queryInput => {
this.query = queryInput
return this._companyService.getCompanies(this.queryRequired + ' ' + this.query, this.page, this.sort);
})
.share();
...
Observable.forkJoin(this.statuses$, this.companies$)
.subscribe(res => {
console.log('forkjoin');
this._countStatus(res[0], res[1]);
});
// This shows arrays in the console...
this.statuses$.subscribe(res => console.log(res));
this.companies$.subscribe(res => console.log(res));
// In the console
Array[9]
Array[6]
推荐答案
forkJoin
的一个很常见的问题是它要求所有源 Observables 至少发出一项,并且所有这些都必须完成.
A very common problem with forkJoin
is that it requires all source Observables to emit at least one item and all of them have to complete.
换句话说,如果 this.statuses$
或 this.companies$
不发出任何项目,直到它们都完成 forkJoin
不会发出任何东西.
In other words if this.statuses$
or this.companies$
doesn't emit any item and until they both complete the forkJoin
won't emit anything.
this.statuses$.subscribe(
res => console.log(res),
undefined,
() => console.log('completed'),
);
这篇关于Observable forkJoin 不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!