本文介绍了可观察到的forkJoin不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在两个Observables上使用用户forkJoin.其中之一以流的形式开始...如果我直接订阅它们,则会收到响应,但forkJoin不会触发.有任何想法吗?

I'm trying to user 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的一个非常普遍的问题是,它要求所有源Observable都发出至少一项,并且所有它们都必须完成.

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'),
);

这篇关于可观察到的forkJoin不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:09