angular - Angular 教程中的 pipe 和 tap 方法是什么?

标签 angular

我正在学习 https://angular.io 上的教程,而且我找不到文档;特别针对方法 pipetap。我在 https://angular.io 上找不到任何内容或 http://reactivex.io/rxjs/ .

我的理解是 pipetap 都是 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/

相关文章:

angular - 如果使用查询参数,则以编程方式将查询参数附加到路由

Angular 5,httpclient 忽略 post 中设置的 cookie

angular - 自定义结构指令创建了太多元素

Angular2 - 当两种服务相互需要时

Angular Material 输入 float 标签默认颜色更改

javascript - Angular 订阅内订阅 : data doesn't load at the same time within view

angular - 我们是否应该停止使用 @View 注释并仅在 Angular 2 组件中使用 @Component ?

css - ionic 3元素中的 Font Awesome 安装

Angular 2 内联编辑器

angular - 使用 mat-option 选择 "null"值,Angular 7 响应式(Reactive)表单不起作用