fromEvent

signature: fromEvent(target: EventTargetLike, eventName: string, selector: function): Observable

Turn event into observable sequence.

fromEvent - 图1

Examples

Example 1: Observable from mouse clicks

( jsBin |
jsFiddle )

  1. import { fromEvent } from 'rxjs/observable/fromEvent';
  2. import { map } from 'rxjs/operators';
  3. //create observable that emits click events
  4. const source = fromEvent(document, 'click');
  5. //map to string with given event timestamp
  6. const example = source.pipe(map(event => `Event time: ${event.timeStamp}`));
  7. //output (example): 'Event time: 7276.390000000001'
  8. const subscribe = example.subscribe(val => console.log(val));

Additional Resources


:file_folder: Source Code:
https://github.com/ReactiveX/rxjs/blob/master/src/internal/observable/FromEventObservable.ts