问题
I have a Observable array similarIdeasObservable$ and another Observable array being getting from server by like this.ideaService.getSimilarIdeas(). I want to merge these two Observable arrays without using subscribe. How Can I do this?
I am trying following way
this.similarIdeasObservable$.pipe(concat(this.ideaService.getSimilarIdeas(this.idea).pipe(
concatMap(i => i),
toArray(),
catchError(error => {
throw error;
}),
finalize(() => {
this.loading = false;
})
)));
concat is deprecated.
回答1:
You can use merge to merge multiple observable.
e.g.
import { merge } from 'rxjs';
const example = merge(
similarIdeasObservable$,
this.ideaService.getSimilarIdeas(this.idea)
);
回答2:
I would recommend you to use forkJoin.
The idea of how forkJoin works is that it requires the input observables (observableA and observableB on the below example) to be completed, and it will eventually be used to return an observable represented by an array, which consists of the values returned by the input observables.
import { forkJoin } from 'rxjs';
const observableA = this.similarIdeasObservable$;
const observableB = this.ideaService.getSimilarIdeas(this.idea);
forkJoin(observableA, observableB);
来源:https://stackoverflow.com/questions/57178820/merge-two-observables-array-into-single-observable-array