RxJS提供了一些用来创建Observable对象的函数,我们可以根据事件、定时器、Promise,AJAX等来创建Observable对象。
下面是根据事件创建Observable对象的几个例子。
this.outsideClick$ = fromEvent(document, 'click').subscribe((event: MouseEvent) => {this.onClick(event);});
fromEvent(window, 'resize').subscribe((e: any) => {this.windowSize = e.target.innerWidth;}),
fromEvent(document, 'mousedown').subscribe((event) => {if (!event['isOpen']) {this.close(null);}}),
fromEvent(window, 'mousewheel').pipe(debounceTime(250)).subscribe((e: any) => {// handle e}}),