角度 7 中的管道和地图之间的确切区别是什么?

man*_*anu 5 javascript rxjs typescript angular

我对此进行了很多搜索,但我找不到angular 7pipemapangular 7之间的区别?是否有必要在 angular 7 的 Service.ts 文件中使用管道?

谢谢

Ami*_*ani 9

使用 rxjs 5.5 及更高版本,如果您想在 observable 上使用任何运算符,您只需管道它们。所以这里map只是pipe.

前任:

const example = source.pipe(map(val => val + 10), first());
Run Code Online (Sandbox Code Playgroud)

对于早期版本rxjs没有pipe关键字,使用.符号组合多个运算符

前任:

const example = source.map(val => val + 10).first();
Run Code Online (Sandbox Code Playgroud)


小智 7

map()、filter()、concat() 和 flatMap() 这些是 RxJS 库提供的运算符。您可以使用管道将这些运算符链接在一起。管道允许您将多个函数组合成一个函数,如下所示:

import { filter, map } from 'rxjs/operators';

const squareOddVals = pipe(
filter((n: number) => n % 2 !== 0),
map(n => n * n)
);
Run Code Online (Sandbox Code Playgroud)


Joh*_*uez 5

简而言之,RxJS中的管道用于拦截结果并使用RxJS运算符修改它,这样当您订阅它时,您将获得与您在管道示例中设置的逻辑运算符maptap等等效的最终结果。

对于角度实践指南,请在此处查看 https://angular.io/guide/rx-library

您可以在此处查看运算符列表 https://www.learnrxjs.io/operators/