本文介绍了无法导入Observable.fromEvent-RXJS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据输入事件创建一个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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-02 21:55