RxJS之转化操作符 ( Angular环境 )
一 map操作符
类似于大家所熟知的 Array.prototype.map 方法,此操作符将投射函数应用于每个值 并且在输出 Observable 中发出投射后的结果。
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators/map';
@Component({
selector: 'app-convert',
templateUrl: './convert.component.html',
styleUrls: ['./convert.component.css']
})
export class ConvertComponent implements OnInit {
constructor() { }
ngOnInit() {
of(1, 2).pipe(map(val => val * 10))
.subscribe(val => {
console.log(val);
});
}
}

二 switchMap操作符
将每个源值映射成 Observable,并输出这个新生成的内部Observable。
源值发生变化时,停止旧的Observable及其订阅,输出新的Observable。
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { interval } from 'rxjs/observable/interval';
import {map} from 'rxjs/operators/map';
import { switchMap } from 'rxjs/operators/switchMap';
@Component({
selector: 'app-convert',
templateUrl: './convert.component.html',
styleUrls: ['./convert.component.css']
})
export class ConvertComponent implements OnInit {
constructor() { }
ngOnInit() {
interval(5000)
.pipe(
switchMap(
val => interval(1000)
.pipe(map(val2 => val * 100 + val2))
)
)
.subscribe(val => {
console.log(val);
});
}
}

三 mergeMap操作符
Returns an Observable that emits items based on applying a function that you supply to each item emitted by the source Observable, where that function returns an Observable, and then merging those resulting Observables and emitting the results of this merger.
import { Component, OnInit } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators/map';
import { mergeMap } from 'rxjs/operators/mergeMap';
@Component({
selector: 'app-combine',
templateUrl: './combine.component.html',
styleUrls: ['./combine.component.css']
})
export class CombineComponent implements OnInit {
constructor() { }
ngOnInit() {
of('dog', 'tiger')
.pipe(mergeMap((outer: string) => {
return of('cat', 'lion').pipe(
map((inner: string) => {
return outer + ' ' + inner;
}));
}))
.subscribe(
(val: string) => {
console.log(val);
}
);
}
}

处理串行ajax请求 ( safari停止跨域限制 )
import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { of } from 'rxjs/observable/of';
import { map } from 'rxjs/operators/map';
import { mergeMap } from 'rxjs/operators/mergeMap';
@Component({
selector: 'app-combine',
templateUrl: './combine.component.html',
styleUrls: ['./combine.component.css']
})
export class CombineComponent implements OnInit {
constructor(private http: HttpClient) { }
ngOnInit() {
this.http.get('https://www.baidu.com', { responseType: 'text' })
.pipe(mergeMap((baidu: string) => {
return this.http.get('https://www.sogou.com', { responseType: 'text' })
.pipe(map((sogou: string) => {
const baiduTitle = baidu.substring(baidu.indexOf('<title>') + 7, baidu.indexOf('</title>'));
const sogouTitle = sogou.substring(sogou.indexOf('<title>') + 7, sogou.indexOf('</title>'));
return [baiduTitle, sogouTitle];
}));
}))
.subscribe((titles: string[]) => {
console.log(titles);
});
}
}

RxJS之转化操作符 ( Angular环境 )的更多相关文章
- RxJS之工具操作符 ( Angular环境 )
一 delay操作符 源Observable延迟指定时间,再开始发射值. import { Component, OnInit } from '@angular/core'; import { of ...
- RxJS之过滤操作符 ( Angular环境 )
一 take操作符 只发出源 Observable 最初发出的的N个值 (N = count). 如果源发出值的数量小于 count 的话,那么它的所有值都将发出.然后它便完成,无论源 Observa ...
- RxJS之组合操作符 ( Angular环境 )
一 merge操作符 把多个 Observables 的值混合到一个 Observable 中 import { Component, OnInit } from '@angular/core'; i ...
- RxJS之Subject主题 ( Angular环境 )
一 Subject主题 Subject是Observable的子类.- Subject是多播的,允许将值多播给多个观察者.普通的 Observable 是单播的. 在 Subject 的内部,subs ...
- RxJS中高阶操作符的全面讲解:switchMap,mergeMap,concatMap,exhaustMap
RxJS中高阶映射操作符的全面讲解:switchMap, mergeMap, concatMap (and exhaustMap) 原文链接:https://blog.angular-universi ...
- dtGrid插件集成到Angular环境实现表格化数据展现
00没有抱怨的世界 周末效率好低,两天没更了,看看这看看那,装了个win10发现触摸板驱动不适配,然后找了好久都不行,23333. AngularJS用的时间很短,高级的用法有点吃不消了,$diges ...
- angular环境搭建时的坑
安装angular环境踩过一些坑,最终还是把工程跑起来了,这里描述一下我的步骤,不排除有些步骤是多余的,希望能对遇到同样问题的小伙伴有帮助. 下载最新版node.js. 安装node,安装过程一路点下 ...
- Rxjs之创建操作符(Angular环境)
一 of操作符 import { Component, OnInit } from '@angular/core'; import { of } from 'rxjs/observable/of'; ...
- Angular环境准备和Angular cli
Angular4.0来了,更小,更快,改动少 接下来为Angular4.0准备环境和学会使用Angular cli项目 1.环境准备: 1)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...
随机推荐
- react-native android 和ios 集成 jpush-react-native 激光推送
安装 $ npm install jpush-react-native --save # jpush-react-native 版本以后需要同时安装 jcore-react-native $ npm ...
- ArrayList、LinkedList、Vector的区别。
1. 对于ArrayList与Vector来说,底层都是采用数组方式来实现的 2. ArrayList,LinkedList是不同步的,即线程不安全,而Vector是的.(线程安不安全) 3. Lin ...
- xlwt模块
python使用xlwt模块操作Excel 该模块安装很简单 $ pip install xlwt 语法 import xlwt # 创建一个workbook 设置编码 workbook = xlwt ...
- java byte[]与十六进制字符串相互转换
http://blog.csdn.net/worm0527/article/details/69939307 http://blog.csdn.net/androiddeveloper_lee/art ...
- java swing 制作一个登陆界面,亲测有效
一.介绍 Swing 是一个为Java设计的GUI工具包. Swing是JAVA基础类的一部分. Swing包括了图形用户界面(GUI)器件如:文本框,按钮,分隔窗格和表. Swing提供许多比AWT ...
- Erlang 笔记
集成开发环境:IntelliJ IDEA的Erlang插件 教程:www.erlang-cn.com/462.html,寻找erlang程序设计第2版pdf f():释放之前绑定过的所有变量. -ex ...
- numpy.distutils.system_info.NotFoundError: no lapack/blas resources found问题解决
操作环境 Python3.6 + Windows7 问题现象 利用pip自动安装seaborn/numpy/scipy(pip install seaborn)模块失败,提示numpy.distu ...
- Ajax与select标签的组合运用
---------------------------------------------------------------------------------------------------- ...
- winform 凹进去的button
如果是工具栏按钮的话,可以设置CheckState属性为CheckState.Checked,这样就是按下状态了如果是普通按钮的话,有两种方法一种是系统提供的,在工具箱上右键,[选择项],然后在[CO ...
- 吴裕雄 python 机器学习-KNN(2)
import matplotlib import numpy as np import matplotlib.pyplot as plt from matplotlib.patches import ...