Import of lettable operators and observable creation methods

Viewed 411

I'm upgrading to Angular 5 and RxJS 5.5.2 and trying to import Observable.of operator.

Before lettable operators, we did it like this:

import 'rxjs/add/observable/of';

// Usage
Observable.of(...)

But now importing from paths containing add is discouraged.

So what is the proper way of importing and using lettable static operators now?

1 Answers

The operators that have now a lettable version are the instance operators.

Since before 5.5.x of and any other observable creation methods can be used as in a static way as follows:

import { of } from 'rxjs/observable/of';

The docs from rxjs are pretty clear on this topic:

You pull in any operator you need from one spot, under 'rxjs/operators' (plural!). It's also recommended to pull in the Observable creation methods you need directly as shown below with range:

import { range } from 'rxjs/observable/range';
import { map, filter, scan } from 'rxjs/operators';

const source$ = range(0, 10);

source$.pipe(
  filter(x => x % 2 === 0),
  map(x => x + x),
  scan((acc, x) => acc + x, 0)
)
.subscribe(x => console.log(x))
Related