【问题标题】:How to Skip the API call using RxJS skip operator?如何使用 RxJS 跳过运算符跳过 API 调用?
【发布时间】:2022-01-17 13:18:24
【问题描述】:

我正在尝试使用 RxJS 中的 skip 运算符跳过第一个 API 调用。但我无法做到这一点。

const source = of('a', 'b', 'c', 'd', 'e');

const example = source.pipe(
  tap(() => console.log('Service call triggered')), // I'm using a switchMap here to trigger the API call , tap is just for explaining the issue
  skip(1)
); 

const subscribe = example.subscribe((val) => console.log(val));

在上面的示例中,我看到 5 console.logs 。但我只想看到 4 个 console.logs 。请问您能帮忙吗?

Stackblitz

【问题讨论】:

  • skip 需要之前 tap 才能真正跳过第一个控制台日志。
  • 操作员顺序在这里很重要。尝试将skip(1) 移到顶部,它应该可以工作,就像提到的@AT82 一样
  • 在 stackblitz 中我只看到来自订阅的 4 个控制台日志
  • @ShamPooSham 很好,似乎顺序是正确的,skiptap 之前 :)

标签: javascript angular typescript rxjs rxjs6


【解决方案1】:

试试下面的代码:

const example = source.pipe(
  skip(1),
  tap(() => console.log('Service call triggered')), 
);

const subscribe = example.subscribe((val) => console.log(val));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多