我对管道运算符和链接地图有点困惑。以下两个例子在功能上是否相同?管道功能的目的或优点是什么?
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));
使用管道的“新”方式称为Lettable Operators
Pipeable Operators.使用“patch运算符”调用链接运算符的“旧”方式。
原文链接:https://www.f2er.com/angularjs/144054.htmlStarting in version 5.5 we have shipped “pipeable operators”,which can be accessed in
rxjs/operators
(notice the pluralized “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/*
.
有一些problems with the patch operators.他们还可以确保您的代码中生成的包更小。还有其他优点,请参阅documentation,它可以很好地涵盖它。
尽管您的2个代码示例在功能上是等效的,但要回答您的其他问题。此外,您应尽可能使用Pipeable Operators而不是Patch Operators。
从documentation(完整性)
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. 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.- Operators patched directly onto the prototype are not “tree-shakeable” by tools like rollup or 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. That means that you might import
scan
,but stop using it,and it’s still being added to your output bundle. With pipeable operators,if you’re not using it,a lint rule can pick it up for you.- 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. You don’t need to extend Observable or override
lift
anymore.