使用一个例子来研究 map 操作符的工作原理。
推荐阅读本文之前,先浏览这篇文章RxJs fromEvent 工作原理分析以了解相关知识。
源代码:
import { Component, OnInit, Inject } from '@angular/core'; import { fromEvent, combineLatest } from 'rxjs'; import { mapTo, startWith, scan, tap, map } from 'rxjs/operators'; import { DOCUMENT } from '@angular/common'; @Component({ selector: 'app-combine-latest', templateUrl: './combine-latest.component.html' }) export class CombineLatestComponent implements OnInit { readonly document: Document; constructor( // https://github.com/angular/angular/issues/20351 @Inject(DOCUMENT) document: any) { this.document = document as Document; } redTotal:HTMLElement; blackTotal: HTMLElement; total:HTMLElement; test:HTMLElement; ngOnInit(): void { this.redTotal = this.document.getElementById('red-total'); this.blackTotal = this.document.getElementById('black-total'); this.total = this.document.getElementById('total'); this.test = this.document.getElementById('test'); combineLatest(this.addOneClick$('red'), this.addOneClick$('black')).subscribe(([red, black]: any) => { this.redTotal.innerHTML = red; this.blackTotal.innerHTML = black; this.total.innerHTML = red + black; }); fromEvent(this.test, 'click').pipe(map( event => event.timeStamp)).subscribe((event) => console.log(event)); } addOneClick$ = id => fromEvent(this.document.getElementById(id), 'click').pipe( // map every click to 1 mapTo(1), // keep a running total scan((acc, curr) => acc + curr, 0), startWith(0) ); }
先缕一缕顺序:
- 首先执行fromEvent,返回一个 Observable 对象。
- 执行 map 操作符,其结果作为输入,传入 pipe