Reputation: 1560
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
error TS2339: Property 'fromEvent' does not exist on type 'typeof Observable'.
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);
});
}
}
Upvotes: 12
Views: 21260
Reputation: 16451
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.
When using fromEvent
use it as a function as follows:
fromEvent(this.elementRef.nativeElement, 'keyup')
Not as a static method as follows (this was correct in previous RxJS versions)
Observable.fromEvent(this.elementRef.nativeElement, 'keyup')
Upvotes: 24