问题描述
我正在尝试从输入事件创建一个 Observable.我已经尝试了几乎所有东西,但无法导入fromEvent".这是我的代码.我正在使用 angular 6.0.1 和 RXJS 6.1.0
I'm trying to create an Observable from an input event. I have already tried almost everything and I can not import "fromEvent". This is my code. I am using angular 6.0.1 and RXJS 6.1.0
错误 TS2339:类型typeof Observable"上不存在属性fromEvent".
import { Directive, EventEmitter, Input, Output, ElementRef, ViewChild, AfterViewInit } from '@angular/core';
import { NgControl } from '@angular/forms';
import { distinctUntilChanged } from 'rxjs/internal/operators/distinctUntilChanged';
import { debounceTime } from 'rxjs/internal/operators/debounceTime';
import { map } from 'rxjs/internal/operators/map';
import { filter } from 'rxjs/internal/operators/filter';
import { Observable } from 'rxjs/internal/Observable';
//import 'rxjs/internal/observable/fromEvent';
//import 'rxjs/add/observable/fromEvent';
@Directive({
selector: '[ngModel][debounceTime]'
})
export class InputDebounceDirective implements AfterViewInit {
@Output()
public onDebounce = new EventEmitter<any>();
@Input('debounceTime')
public debounceTime: number = 1500;
constructor(private elementRef: ElementRef, private currentModel: NgControl) { }
ngAfterViewInit() {
Observable.fromEvent(this.elementRef.nativeElement, 'keyup')
.pipe(
debounceTime(this.debounceTime),
distinctUntilChanged()
)
.subscribe(x => {
this.onDebounce.emit(x);
});
}
}
推荐答案
你必须在 RxJS6 中导入 fromEvent
如下:
You must import fromEvent
as follows in RxJS6:
import {fromEvent} from 'rxjs';
Read the migration guide for further information, in particular have a look on the import paths section there.
当使用 fromEvent
时,将其用作函数,如下所示:
When using fromEvent
use it as a function as follows:
fromEvent(this.elementRef.nativeElement, 'keyup')
不是如下的静态方法(这在以前的 RxJS 版本中是正确的)
Not as a static method as follows (this was correct in previous RxJS versions)
Observable.fromEvent(this.elementRef.nativeElement, 'keyup')
这篇关于无法导入 Observable.fromEvent - RXJS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!