rxjs里switchMap operators的用法
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的用法相关推荐
- rxjs里concatMap operators的用法
Projects each source value to an Observable which is merged in the output Observable, in a serialize ...
- rxjs里delay operators的用法
Delays the emission of items from the source Observable by a given timeout or until a given Date. 例子 ...
- rxjs里debounceTime operators的用法
Emits a value from the source Observable only after a particular time span has passed without anothe ...
- rxjs里withLatestFrom operators的用法
Combines the source Observable with other Observables to create an Observable whose values are calcu ...
- rxjs里combineLatest operators的用法
Whenever any input Observable emits a value, it computes a formula using the latest values from all ...
- rxjs里scan operators的用法
Applies an accumulator function over the source Observable, and returns each intermediate result, wi ...
- rxjs里mapTo operators的用法
和map工作原理类似,只不过emit的是一个常数. Like map, but it maps every source value to the same output value every ti ...
- rxjs里merge operators的用法
stream是lazy的,no subscription, no calculation occurs. 作用:Flattens multiple Observables together by bl ...
- Rxjs 里 filter(Boolean) 的用法
StackOverflow 上的讨论: https://stackoverflow.com/questions/53953015/using-rxjs-with-filterboolean-for-q ...
最新文章
- SQL函数Group_concat用法
- Java axis 配置host_Java AxisProperties类代码示例
- FastJSON 简介及其Map/JSON/String 互转(转载)
- 李宏毅机器学习(九)Multi-lingual BERT
- html5 客户端数据缓存机制,深入理解HTML5离线缓存机制
- 国产机器人销售增涨 安防市场有待挖掘
- 自己组装服务器计算机,深度学习服务器组装经验过程分享
- history指令显示日期时间
- python通讯录文件读取_python通用读取vcf文件的类(可以直接复制粘贴使用)
- J2EE项目数据字典设计
- 服务器CPU使用率过高排查与解决思路
- 《HarmonyOS开发 – OpenHarmony开发笔记(基于小型系统)》第1章 OpenHarmony与Pegasus物联网开发套件简介
- PFSO-T5,一种OLED材料
- Adaboost入门教程——最通俗易懂的原理介绍(图文实例)
- Word2016查找和替换通配符(完全版)
- 布局文件:报警告 This inspection highlights unknown XML attributes in Android resource files and Andro...
- argc,**argv
- 联想天逸310-15ikb装固态内存和重装系统
- php生成本地word文件怎么打开,php生成word文件的简单范例
- RestfulToolKit:便捷的 IDEA 接口测试神器