Rxjs,带有一个参数的管道

Rxjs, Pipe with one argument

使用带有一个参数的 Pipe 函数与根本不使用 Pipe 有什么不同吗?

我目前正在实施 . In the "official solution" from the takeUntil operator is sent through a pipe. However, on this page 的 takeUntil 取消订阅策略 takeUntil 不使用管道。

因此,我想知道使用具有单个 Rx 运算符的 Pipe 与根本不使用 Pipe 是否有任何区别(内存 leakage/performance,等等)。

private destroy$ = new Subject();
...
this.potatoService.getPotato()
   .pipe(
    takeUntil(this.destroy$)
   ).subscribe(...

相对于

this.potatoService.getPotato()
    .takeUntil(this.destroy$)
    .subscribe(...

没有区别。后者是在 RxJS 中使用运算符的旧方法。但是,据我所知,它已被弃用,您不应该使用它。

我们曾经使用这样的静态导入将运算符原型化为 Observables

import 'rxjs/add/operator/takeUntil';

然而,这使得 tree shake 变得不可能 RxJS。因此,RxJS 宣布从 RxJS v5.5 开始的管道运算符。始终在 pipe

内使用您的运算符

从 RxJS v6 开始,takeUntil(和其他)已经成为可管道操作符,而不是一个独立的函数。

在您分享的 link 中,请查看导入部分,这意味着此示例使用了旧版本的 RxJS:

import 'rxjs/add/operator/takeUntil';

根据RxJS v6官方文档,takeUntil的导入路径变为:

import { takeUntil } from 'rxjs/operators';

进一步阅读:https://rxjs-dev.firebaseapp.com/api/operators/takeUntil

重点是老办法是把运算符加到原型上,这样每个可观察的实例都可以使用它。这就是为什么它使运算符不可摇晃,并且不鼓励采用这种方式。