【问题标题】:Differences between observable.takeUntil... and observable.pipe(takeUntilobservable.takeUntil... 和 observable.pipe(takeUntil
【发布时间】:2021-03-09 09:26:41
【问题描述】:

你能解释一下 observable.takeUntil 和 observable.pipe(takeUntil...) 有什么区别吗?

例子:

this.api
        .testMethod(
            this.queryParam,
        )
        .takeUntil(this.destroy$)
        .subscribe(

this.api
        .testMethod(
            this.queryParam,
        )
        .pipe(takeUntil(this.destroy$))
        .subscribe(

当我写第一个选项时,我看到我需要从 'rxjs/operators' 导入...

【问题讨论】:

  • 方法.takeUntil是RxJS 5方法,管道函数.pipe(takeUntil是RxJS 6

标签: reactjs angular rxjs


【解决方案1】:

它主要归结为使用的 RxJS 版本。 RxJS v5 和更早的版本允许链接运算符而不需要pipe。 RxJS v6+ 需要明确提及 pipe

import 'rxjs/add/operator/takeUntil';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/tap';

this.api.testMethod(this.queryParam)
  .takeUntil(this.destroy$)
  .map(data => data)
  .tap(console.log)
  .subscribe();

RxJS v6+

import { takeUntil, map, tap } from 'rxjs/operators';

this.api.testMethod(this.queryParam).pipe(
  takeUntil(this.destroy$),
  map(data => data),
  tap(console.log)
).subscribe();

【讨论】:

    猜你喜欢
    • 2019-10-14
    • 1970-01-01
    • 2020-07-25
    • 2019-12-18
    • 2015-09-30
    • 2019-11-15
    • 2018-07-30
    • 1970-01-01
    • 2014-04-23
    相关资源
    最近更新 更多