Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import of lettable operators and observable creation methods

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?

like image 646
Alexander Abakumov Avatar asked Dec 06 '25 07:12

Alexander Abakumov


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))
like image 127
Jota.Toledo Avatar answered Dec 07 '25 20:12

Jota.Toledo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!