3

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?

Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129

1 Answers1

5

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))
Jota.Toledo
  • 27,293
  • 11
  • 59
  • 73
  • `now a lettable version are the instance operators` not sure how to use them now. Could you post an example of creating an `Observable` from a constant value of `1`? Also, what if we need to import multiple static operators e.g. `of` and `from` - is there a way to combine the imports in one line? – Alexander Abakumov Nov 08 '17 at 21:08
  • updated, and nope, there is no barrel file on rxjs/observable, you need to import one by one – Jota.Toledo Nov 08 '17 at 21:12