RxJS 中的 map 与 switchMap [英] map vs switchMap in RxJS

查看:47
本文介绍了RxJS 中的 map 与 switchMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我阅读了 switchMapmap,但我仍然不完全理解其中的区别.是否在某些情况下它根本没有区别?

I read the documentation of switchMap and map, but I still don't completely understand the difference. Are there some cases where it does not make a difference at all?

推荐答案

两种操作符不同.

switchMap:将值映射到 observable.取消之前的内部 observable.

switchMap: Maps values to observable. Cancels the previous inner observable.

例如:

fromEvent(document, 'click')
  .pipe(
    // restart counter on every click
    // First click: 0, 1, 2...
    // Second click: cancels the previous interval and starts new one. 0, 1, 2...
    switchMap(() => interval(1000))
  )
  .subscribe(console.log);

ma​​p:为每个值添加投影.

例如:

//add 10 to each value
const example = source.pipe(map(val => val + 10));

这篇关于RxJS 中的 map 与 switchMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆