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)在开始工作之前我们必须设置好开发环境 如果你的机器上还没有安 ...
随机推荐
- grep命令的常用选项
Linux的grep命令是使用正则表达式进行文本搜索的,一些对程序员很有用的选项如下: -i 忽略大小写 -w 进行普通文件匹配,而不是正则表达式匹配 -c 只统计每个文件中匹配行数(默认是输 ...
- VC 字符串转化和分割
原文:点击这里. 备忘:为了适用于Unicode环境,要养成使用_T()宏的习惯 1.格式化字符串 CString s;s.Format(_T("The num is %d."), ...
- day42-多线程与多进程
一 进程与线程的概念 1.1 进程 考虑一个场景:浏览器,网易云音乐以及notepad++ 三个软件只能顺序执行是怎样一种场景呢?假如有两个程序A和B,程序A在执行到一半的过程中,需要读取大量的数据输 ...
- WDA-3-ALV查询
主要是梳理下WebDynpro For ABAP开发过程: 1.创建WebDynpro组件 2.创建WebDynpro应用 1.创建WebDynpro组件 1.1创建 路径:选择Package--&g ...
- CSS VISUAL RULES
CSS VISUAL RULES Review Visual Rules Incredible work! You used CSS to alter text and images througho ...
- 1016B - Segment Occurrences(字符串的匹配)
题意:字符串a,字符串b,给你q个区间,让你求a的区间内字符串b出现了多少次 之前用的前缀数组做的,没想起来,发现这个其实也可以 #include<cstdio> #include< ...
- weka连接mysql数据库
一.下载并解压数据库驱动 下载地址:http://www.mysql.com/products/connector/,本文下载 mysql-connector-java-5.0.8.zip.将解压后的 ...
- linux下给PHP安装拓展
要先完成了上一篇文章的phpize的操作,并激活它才能下一步. 下载拓展,在http://pecl.php.net/这个网站下载,其他的有可能不成功,我之前还很纳闷phpize已经调试通过了,但是切换 ...
- java Run to Line
在运行Java代码时, 选择运行 Run AS java , 出现 java Run to Line, 是因为程序还在运行,没有停止.在控制,点击右键.terminate 结束,在Run ...
- thread == 售票
import org.apache.xerces.util.SymbolTable; public class ThreadDemo1 { public static void main(String ...