我在https://angular.io岁时就开始学习教程了,但我很难找到文档;特别是对于方法pipe
和tap
.我在https://angular.io或http://reactivex.io/rxjs/上找不到任何东西.
我的理解是pipe
和tap
都是Observable
的方法,都是从RxJS导入的,对吗?他们该怎么办?
这些方法是Angular的一部分吗?这两种方法的作用是什么?
我在https://angular.io岁时就开始学习教程了,但我很难找到文档;特别是对于方法pipe
和tap
.我在https://angular.io或http://reactivex.io/rxjs/上找不到任何东西.
我的理解是pipe
和tap
都是Observable
的方法,都是从RxJS导入的,对吗?他们该怎么办?
这些方法是Angular的一部分吗?这两种方法的作用是什么?
你说得对,文档中缺少这些方法.然而,当我深入研究rxjs存储库时,我发现了大约tap条(太长了,无法在这里粘贴)和pipe个操作员的好 comments :
/**
* 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))
*/
Pipe:用于将函数运算符拼接成链.以前我们只能做observable.filter().map().scan()
,但是因 for each RxJS操作符都是一个独立的函数,而不是可观察对象的方法,所以我们需要pipe()
来构建这些操作符的链(参见上面的示例).
Tap:可以对观察到的数据执行副作用,但does not modify可以以任何方式对流执行副作用.以前叫do()
.你可以把它想象成一个数组,随着时间的推移,tap()
相当于Array.forEach()
.