41

In RxJS 6, how do I import a static merge function for merging a list of Observables?

I want to be able to do:

const merged$ = merge(
    obs1$,
    obs2$,
    obs3$
);

I've tried:

import { merge } from 'rxjs/observable/merge'; and

import { merge } from 'rxjs/operators';

but neither seems to give me what I want.

Praveen Gehlot
  • 364
  • 2
  • 11
bingles
  • 11,582
  • 10
  • 82
  • 93
  • See that example in the doc : https://www.learnrxjs.io/operators/combination/merge.html#example-1-merging-multiple-observables-static-method – Guillaume Husta Dec 19 '19 at 10:26

4 Answers4

42

Importing has been made easy in RxJS 6:

 import { merge } from 'rxjs';

You may want to read the official migration guide.

Another useful resource regarding importing in RxJS 6 is this talk by Ben Lesh who is the RxJS lead.

Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
siva636
  • 16,109
  • 23
  • 97
  • 135
7

I believe now when the "creation" classes were removed the recommended way is importing directly from 'rxjs':

import { merge as mergeStatic } from 'rxjs';

Previous alpha version of RxJS 6 used to have 'rxjs/create' file but this has been removed already: https://github.com/ReactiveX/rxjs/blob/master/CHANGELOG.md#600-alpha3-2018-02-06

However this expects you to use path maps correctly otherwise you'll import a lot of things you don't need. If you don't use path maps or the build process hidden from you you can import directly the correct file:

import { merge as mergeStatic } from 'rxjs/internal/observable/merge';
martin
  • 93,354
  • 25
  • 191
  • 226
7

RxJS 7.X

In RxJS v7.X the merge() method is deprecated and will be removed from RxJs v8.X, use mergeWith() instead.

See:
https://rxjs.dev/api/operators/mergeWith
https://rxjs.dev/api/operators/merge (deprecated)

import { fromEvent } from 'rxjs';
import { map, mergeWith } from 'rxjs/operators';

const clicks$ = fromEvent(document, 'click').pipe(map(() => 'click'));
const mousemoves$ = fromEvent(document, 'mousemove').pipe(map(() => 'mousemove'));
const dblclicks$ = fromEvent(document, 'dblclick').pipe(map(() => 'dblclick'));

mousemoves$.pipe(
  mergeWith(clicks$, dblclicks$),
)
.subscribe(x => console.log(x));

// result (assuming user interactions)
// "mousemove"
// "mousemove"
// "mousemove"
// "click"
// "click"
// "dblclick"

(example from api docs)

Gil Epshtain
  • 8,670
  • 7
  • 63
  • 89
0

As of RXJS 6. The merge is in the rxjs/operators

import { map, take, merge, switchMap, filter } from 'rxjs/operators';
Francesco Borzi
  • 56,083
  • 47
  • 179
  • 252
Kevin Summersill
  • 642
  • 2
  • 8
  • 22
  • 5
    Not exactly. There are two merge functions: from 'rxjs': https://rxjs-dev.firebaseapp.com/api/index/function/merge and from 'rxjs/operators': https://rxjs-dev.firebaseapp.com/api/operators/merge – ViES Sep 07 '18 at 12:15
  • 3
    The difference: import { merge } from 'rxjs' -> Creates an output Observable which concurrently emits all values from every given input Observable. || import { merge } from 'rjxs/operators' -> Deprecated in favor of static merge. You are correct; start using just 'rxjs' https://github.com/ng-bootstrap/ng-bootstrap/issues/2394 – Kevin Summersill Sep 08 '18 at 18:51