Import of lettable operators and observable creation methods

徘徊边缘 提交于 2019-12-13 15:23:11

问题


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:


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))


来源:https://stackoverflow.com/questions/47189540/import-of-lettable-operators-and-observable-creation-methods

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!