tap 和 map 都是 RxJS 运算符,RxJS 运算符只是对数据执行一些操作的函数。
它们都是pipeable operators,它将输入作为 Observable,执行一些操作并返回一个输出 observable。
地图和点击的区别:
map 是一个管道操作符,它接受一个输入 observable,对其执行一些操作并返回一个新的操作 observable。例如
const source$ = of(1,2,3) // observable which will emit 1,2,3
// It take an input observable and return a new observable which will emit square of input values.
// So, the output observable will emit 1,4,9
const mapSource$ = of(1,2,3)
.pipe(map(value => value * value))
另一方面,tap 运算符接受输入 observable 执行某些操作并返回相同的输入 observable。
const source$ = of(1,2,3) // observable which will emit 1,2,3
// It take an input observable and return a same observable after console value.
// So, the output observable will emit 1,2,3
const tapSource$ = of(1,2,3)
.pipe(tap(value => console.log(value)))