Angular教程中的管道和水龙头方法是什么? [英] What are pipe and tap methods in Angular tutorial?
问题描述
我正在按照 https://angular.io 上的指南进行操作,但是在查找文档时遇到了麻烦;专门用于方法pipe
和tap
.我在 https://angular.io 或
I'm following the tutorial at https://angular.io, and I'm having trouble finding documentation; specifically for the methods pipe
and tap
. I can't find anything on https://angular.io or http://reactivex.io/rxjs/.
我的理解是pipe
和tap
都是从RxJS导入的Observable
的方法,对吗?他们应该怎么办?
My understanding is that pipe
and tap
are both methods of Observable
, which is being imported from RxJS, correct? What are they supposed to do?
这些方法是Angular的一部分吗?这两种方法是做什么的?
Are these methods part of Angular? What do these two methods do?
推荐答案
是的,文档中缺少这些方法.但是,当我深入rxjs存储库时,发现了关于管道运算符:
You are right, the documentation lacks of those methods. However when I dug into rxjs repository, I found nice comments about tap (too long to paste here) and pipe operators:
/**
* Used to stitch together functional operators into a chain.
* @method pipe
* @return {Observable} the Observable result of all of the operators having
* been called in the order they were passed in.
*
* @example
*
* import { map, filter, scan } from 'rxjs/operators';
*
* Rx.Observable.interval(1000)
* .pipe(
* filter(x => x % 2 === 0),
* map(x => x + x),
* scan((acc, x) => acc + x)
* )
* .subscribe(x => console.log(x))
*/
简介:
管道:用于将功能操作符缝合在一起.在开始做observable.filter().map().scan()
之前,但是由于每个RxJS运算符都是独立的函数,而不是Observable的方法,因此我们需要pipe()
来构成这些运算符的链(请参见上面的示例).
In brief:
Pipe: Used to stitch together functional operators into a chain. Before we could just do observable.filter().map().scan()
, but since every RxJS operator is a standalone function rather than an Observable's method, we need pipe()
to make a chain of those operators (see example above).
点击:可以对观察到的数据产生副作用,但不以任何方式修改.以前称为do()
.您可以将其视为随时间推移可观察的数组,那么tap()
将等效于Array.forEach()
.
Tap: Can perform side effects with observed data but does not modify the stream in any way. Formerly called do()
. You can think of it as if observable was an array over time, then tap()
would be an equivalent to Array.forEach()
.
这篇关于Angular教程中的管道和水龙头方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!