switchMap相关文章

  • rxjs里switchMap operators的用法
  • 通过rxjs的一个例子, 来学习SwitchMap的使用方法
  • rxjs switchMap的实现原理
  • rxjs的map和switchMap在SAP Spartacus中的应用 -将高阶Observable进行flatten操作

Projects each source value to an Observable which is merged in the output Observable, emitting values only from the most recently projected Observable.

理解记忆法:switchMap -> switch to a new Observable,亦即SwitchMap返回的OperatorFunction,其function的输出是一个新的Observable.

例子:

const switched = of(1, 2, 3).pipe(switchMap((x: number) => of(x, x ** 2, x ** 3)));switched.subscribe(x => console.log(x));

输出:

/*const switched = of(1, 2, 3).pipe(switchMap((x: number) => of(x, x ** 2, x ** 3)));switched.subscribe(x => console.log(x));*/const origin = of(1, 2, 3);// fn是一个函数,接受一个number输入,返回一个Observable对象,包含3个number元素const fn = (x: number) => of(x, x ** 2, x ** 3);// OperatorFunction:尖括号里前一个number是输入类型// 第二个括号是输出类型const mapper: OperatorFunction<number, number> = switchMap(fn);// pipe需要接受的类型是OperatorFunction,经过operator传入一个// funcion进去组合而成const switched = origin.pipe(mapper);switched.subscribe(x => console.log('diablo: ' + x));

总结:OperatorFunction的类型很形象,由Operator接收一个function作为输入,组装而成

switchMapTo

const origin = of(111, 222, 333);const copy = origin.pipe(map( (x) => x + 1));// observable: ObservableInput<R>const int = interval(1000).pipe(take(3));const fn = (x: number) => of(x, x ** 2, x ** 3);// 需要传一个Observable进去const mapper = switchMapTo(int);const switched = origin.pipe(mapper);switched.subscribe(x => console.log('diablo2: ' + x));

测试结果:origin里的value完全没有被考虑:

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

rxjs里switchMap operators的用法相关推荐

  1. rxjs里concatMap operators的用法

    Projects each source value to an Observable which is merged in the output Observable, in a serialize ...

  2. rxjs里delay operators的用法

    Delays the emission of items from the source Observable by a given timeout or until a given Date. 例子 ...

  3. rxjs里debounceTime operators的用法

    Emits a value from the source Observable only after a particular time span has passed without anothe ...

  4. rxjs里withLatestFrom operators的用法

    Combines the source Observable with other Observables to create an Observable whose values are calcu ...

  5. rxjs里combineLatest operators的用法

    Whenever any input Observable emits a value, it computes a formula using the latest values from all ...

  6. rxjs里scan operators的用法

    Applies an accumulator function over the source Observable, and returns each intermediate result, wi ...

  7. rxjs里mapTo operators的用法

    和map工作原理类似,只不过emit的是一个常数. Like map, but it maps every source value to the same output value every ti ...

  8. rxjs里merge operators的用法

    stream是lazy的,no subscription, no calculation occurs. 作用:Flattens multiple Observables together by bl ...

  9. Rxjs 里 filter(Boolean) 的用法

    StackOverflow 上的讨论: https://stackoverflow.com/questions/53953015/using-rxjs-with-filterboolean-for-q ...

最新文章

  1. SQL函数Group_concat用法
  2. Java axis 配置host_Java AxisProperties类代码示例
  3. FastJSON 简介及其Map/JSON/String 互转(转载)
  4. 李宏毅机器学习(九)Multi-lingual BERT
  5. html5 客户端数据缓存机制,深入理解HTML5离线缓存机制
  6. 国产机器人销售增涨 安防市场有待挖掘
  7. 自己组装服务器计算机,深度学习服务器组装经验过程分享
  8. history指令显示日期时间
  9. python通讯录文件读取_python通用读取vcf文件的类(可以直接复制粘贴使用)
  10. J2EE项目数据字典设计
  11. 服务器CPU使用率过高排查与解决思路
  12. 《HarmonyOS开发 – OpenHarmony开发笔记(基于小型系统)》第1章 OpenHarmony与Pegasus物联网开发套件简介
  13. PFSO-T5,一种OLED材料
  14. Adaboost入门教程——最通俗易懂的原理介绍(图文实例)
  15. Word2016查找和替换通配符(完全版)
  16. 布局文件:报警告 This inspection highlights unknown XML attributes in Android resource files and Andro...
  17. argc,**argv
  18. 联想天逸310-15ikb装固态内存和重装系统
  19. php生成本地word文件怎么打开,php生成word文件的简单范例
  20. RestfulToolKit:便捷的 IDEA 接口测试神器

热门文章

  1. IfSpeed 带宽计算
  2. [python]pythonic的字典常用操作
  3. 个体重构:将重构应用到生活中
  4. Entity Framework 实体框架的形成之旅--为基础类库接口增加单元测试,对基类接口进行正确性校验(10)...
  5. C#中的集合学习笔记
  6. WCF,简单而又复杂的东西
  7. 漂浮窗口拖动杂谈(续)
  8. 【SF】开源的.NET CORE 基础管理系统 -介绍篇
  9. 12月9日 perform使用入门
  10. js 的 math 函数