rxjs fromEvent的用法
生活随笔
收集整理的這篇文章主要介紹了
rxjs fromEvent的用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
源代碼:
import { of, fromEvent, interval, Observable } from 'rxjs'; import { Injectable } from '@angular/core'; import { map, switchMap } from 'rxjs/operators'; import { tap } from 'rxjs/operators'; import { OperatorFunction } from 'rxjs';@Injectable() export class JerrySandBoxService{name = 'Jerry';jerryMap: OperatorFunction<Event, number> = switchMap(this.jerryintervalFunction);jerryintervalFunction(event: Event){console.log('event: ' + event.timeStamp );/*returns an Observable that emits an infinite sequence of ascending integers, with a constant interval of time of your choosing between those emissions.*///const a = interval(1000);//return a;// return event.timeStamp;return of(event.timeStamp);}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('in subscribe: ' + data));*/const clicks: Observable<Event> = fromEvent(document, 'click');const result = clicks.pipe(this.jerryMap);//result.subscribe(x => console.log(x));result.subscribe(x => {console.log(x);});} }每點擊瀏覽器顯示的窗口一次,就能顯示點擊事件發生時的時間戳:
總結
以上是生活随笔為你收集整理的rxjs fromEvent的用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用代码查看SAP Spartacus购物
- 下一篇: 抖音火山版喜欢列表怎么隐藏