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

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

问题描述

我阅读了 switchMap

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 :将值映射为可观察值.取消先前的内部可观测值.

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);

地图:为每个值添加投影.

map: Add projection with each value.

例如:

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

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

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