导入可操作的运算符和可观察的创建方法 [英] Import of lettable operators and observable creation methods

查看:93
本文介绍了导入可操作的运算符和可观察的创建方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在升级到Angular 5和RxJS 5.5.2,并尝试导入Observable.of运算符.

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

但是现在不建议从包含add的路径导入.

But now importing from paths containing add is discouraged.

那么现在导入和使用可出租的静态运算符的正确方法是什么?

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

推荐答案

现在具有可释放版本的运算符是实例运算符.

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

5.5.x of以来,以及任何其他可观察的创建方法,可以按如下静态方式使用:

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';

docs >在此主题上非常清楚:

The docs from rxjs are pretty clear on this topic:

您可以从"rxjs/operators"(复数!)下的某个位置提取所需的任何运算符.还建议直接使用所需的Observable创建方法,如下所示,范围为:

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

这篇关于导入可操作的运算符和可观察的创建方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆