[英]In Angular rxjs when should I use `pipe` vs `map`
I'm a bit confused by the pipe
operator vs just chaining map
.我对
pipe
运算符与仅链接map
感到有些困惑。 Are both of the examples below functionally equivalent?下面的两个示例在功能上是否相同? What is the purpose or advantage of the pipe function?
管道功能的目的或优势是什么?
const name = ajax
.getJSON<{ name: string }>("/api/employees/alice")
.pipe(
retry(3, 1000),
map(employee => employee.name),
catchError(error => of(null))
);
const name = ajax
.getJSON<{ name: string }>("/api/employees/alice")
.let(retry(3, 1000))
.map(employee => employee.name)
.catch(error => Rx.Observable.of(null));
The "new" way, using pipe
, is called使用
pipe
的“新”方式被称为Lettable OperatorsPipeable Operators .出租经营者
可管道操作符。 The "old" way, where you chain operators, is called using "patch operators".
链接运算符的“旧”方式称为使用“补丁运算符”。
Starting in version 5.5 we have shipped "pipeable operators", which can be accessed in
rxjs/operators
(notice the pluralized "operators").从 5.5 版开始,我们提供了“可管道操作符”,可以在
rxjs/operators
访问(注意复数的“operators”)。 These are meant to be a better approach for pulling in just the operators you need than the "patch" operators found inrxjs/add/operator/*
.与在
rxjs/add/operator/*
找到的“补丁”运算符相比,这些是一种更好的方法,可以只rxjs/add/operator/*
您需要的运算rxjs/add/operator/*
。
There were some problems with the patch operators .补丁操作员有一些问题。 They can also ensure that your produced bundle from your code is smaller.
他们还可以确保您的代码生成的包更小。 There are other advantages as well, see the documentation which fairly well covers it.
还有其他优点,请参阅相当不错的文档。
To answer your other question though your 2 code samples are functionally equivalent.尽管您的 2 个代码示例在功能上是等效的,但要回答您的其他问题。 Also you should use Pipeable Operators over Patch Operators whenever possible.
此外,您应该尽可能使用可管道操作符而不是补丁操作符。
From the documentation ( for completeness )从文档(为了完整性)
Problems with the patched operators for dot-chaining are:
用于点链的修补运算符的问题是:
- Any library that imports a patch operator will augment the
Observable.prototype
for all consumers of that library, creating blind dependencies.任何导入补丁操作符的库都会为该库的所有使用者增加
Observable.prototype
,从而创建盲依赖。 If the library removes their usage, they unknowingly break everyone else.如果图书馆删除了他们的使用,他们会在不知不觉中破坏其他人。 With pipeables, you have to import the operators you need into each file you use them in.
使用 pipeables,您必须将所需的运算符导入到使用它们的每个文件中。
- Operators patched directly onto the prototype are not "tree-shakeable" by tools like rollup or webpack.
直接修补到原型上的操作符不能被 rollup 或 webpack 等工具“摇树”。 Pipeable operators will be as they are just functions pulled in from modules directly.
可管道操作符将是直接从模块中提取的函数。
- Unused operators that are being imported in apps cannot be detected reliably by any sort of build tooling or lint rule.
任何类型的构建工具或 lint 规则都无法可靠地检测到应用程序中导入的未使用运算符。 That means that you might import
scan
, but stop using it, and it's still being added to your output bundle.这意味着您可能会导入
scan
,但停止使用它,它仍然会被添加到您的输出包中。 With pipeable operators, if you're not using it, a lint rule can pick it up for you.对于可管道操作符,如果您不使用它,lint 规则可以为您挑选它。
- Functional composition is awesome.
功能组合很棒。 Building your own custom operators becomes much, much easier, and now they work and look just like all other operators from rxjs.
构建您自己的自定义运算符变得更加容易,现在它们的工作方式和外观与 rxjs 中的所有其他运算符一样。 You don't need to extend Observable or override
lift
anymore.您不再需要扩展 Observable 或覆盖
lift
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.