fromEvent

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

将事件转换成 observable 序列。

示例

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

( StackBlitz | jsBin | jsFiddle )

// RxJS v6+
import { fromEvent } from 'rxjs';
import { map } from 'rxjs/operators';

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

相关食谱

其他资源


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

results matching ""

    No results matching ""