源代碼:
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);
});
}
}