我正在学习 https://angular.io 上的教程,而且我找不到文档;特别针对方法 pipe
和 tap
。我在 https://angular.io 上找不到任何内容或 http://reactivex.io/rxjs/ .
我的理解是 pipe
和 tap
都是 Observable
的方法,是从 RxJS 导入的,对吗?他们该怎么办?
这些方法是 Angular 的一部分吗?这两个方法有什么作用?
最佳答案
你是对的,文档中没有这些方法。然而,当我深入研究 rxjs 存储库时,我发现了关于 tap 的不错的评论。 (此处粘贴太长)和 pipe运营商:
/**
* 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()
来制作这些运算符的链(请参见上面的示例)。
Tap:可以对观察到的数据执行副作用,但不会修改流以任何方式。以前称为 do()
。你可以把它想象成 observable 是一个随时间变化的数组,那么 tap()
就等同于 Array.forEach()
。
关于angular - Angular 教程中的 pipe 和 tap 方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47275385/