通过一个最简单的例子,理解Angular rxjs里的Observable对象的pipe方法
生活随笔
收集整理的這篇文章主要介紹了
通过一个最简单的例子,理解Angular rxjs里的Observable对象的pipe方法
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
源代碼:
import { of } from 'rxjs'; import { Injectable } from '@angular/core'; import { map } from 'rxjs/operators'; import { tap } from 'rxjs/operators';@Injectable() export class JerrySandBoxService{name = 'Jerry';print(){const observable = of(1, 2, 3);const newObservable = observable.pipe(tap(num => console.log(num)),map(num => 'hello world: ' + num));newObservable.subscribe(data => console.log(data));} }執(zhí)行pipe之前先執(zhí)行map函數(shù):
map返回一個(gè)新的函數(shù):
然后執(zhí)行pipe函數(shù):…operations變長(zhǎng)參數(shù),包含tap和map兩個(gè)操作。
pipeFromArray的實(shí)現(xiàn)借助了JavaScript的closure閉包:
到目前為止,我們傳入的tap和map里的箭頭函數(shù)都未執(zhí)行,直到subscribe方法的調(diào)用:
此時(shí)對(duì)于數(shù)組里的1,2,3,先執(zhí)行管道里的tap和map操作,再把map操作的輸出,作為輸入去執(zhí)行subscribe里指定的回調(diào):
要獲取更多Jerry的原創(chuàng)文章,請(qǐng)關(guān)注公眾號(hào)"汪子熙":
總結(jié)
以上是生活随笔為你收集整理的通过一个最简单的例子,理解Angular rxjs里的Observable对象的pipe方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 别骂了别骂了 米哈游新作文案极度扎心 网
- 下一篇: SAP Spartacus CmsCom