简体   繁体   English

在 Angular rxjs 中,我什么时候应该使用 `pipe` 和 `map`

[英]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 Operators出租经营者Pipeable 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 in rxjs/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:用于点链的修补运算符的问题是:

  1. 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,您必须将所需的运算符导入到使用它们的每个文件中。
  2. 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.可管道操作符将是直接从模块中提取的函数。
  3. 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 规则可以为您挑选它。
  4. 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.

相关问题 Angular 5-&gt; Angular 6 Rxjs .map()到.pipe(map()) - Angular 5 -> Angular 6 Rxjs .map() to .pipe(map()) 如何在地图管道RXJS(Angular)中模拟错误? - How to emulate error in map pipe RXJS (Angular)? Angular rxjs映射管道给出了“此”上下文问题 - Angular rxjs map pipe gives 'this' context issue @ angular / fire和RxJS:从rxjs管道/地图返回值 - @angular/fire and RxJS: Return value from rxjs pipe/map 订阅时不会触发 rxjs 管道点击/映射 - rxjs pipe tap/map is not triggered when subscribe 在Rxjs中使用管道时,Observable不订阅 - Angular - Observable not subscribing when using pipe in Rxjs - Angular Angular,Ionic:具有MAP的RxJs Pipe在Windows中不返回值,但在与httpclient一起使用时在Mac中工作 - Angular, Ionic: RxJs Pipe with MAP does not return value in windows, but works in mac when used with httpclient 我应该在Angular中使用switchMap,flatMap还是任何其他rxjs运算符 - Should i use switchMap, flatMap or any other rxjs operator in Angular 我应该对 RxJs 和 Angular 中的主题使用“$”符号后缀吗? - Should I use "$" sign suffix for Subjects in the RxJs and Angular? Angular/RxJS 我什么时候应该取消订阅`Subscription` - Angular/RxJS When should I unsubscribe from `Subscription`
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM