fromEvent

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

将事件转换成 observable 序列。

fromEvent - 图1

示例

示例 1: 鼠标事件转换而来的 observable

( jsBin |
jsFiddle )

  1. import { fromEvent } from 'rxjs/observable/fromEvent';
  2. import { map } from 'rxjs/operators';
  3. // 创建发出点击事件的 observable
  4. const source = fromEvent(document, 'click');
  5. // 映射成给定的事件时间戳
  6. const example = source.pipe(map(event => `Event time: ${event.timeStamp}`));
  7. // 输出 (示例中的数字以运行时为准): 'Event time: 7276.390000000001'
  8. const subscribe = example.subscribe(val => console.log(val));

相关食谱

其他资源


:file_folder: 源码: https://github.com/ReactiveX/rxjs/blob/master/src/observable/FromEventObservable.ts