angular2 Observable 属性 'debouceTime' 在类型 'Observable<any>' 上不存在

angular2 Observable Property 'debouceTime' does not exist on type 'Observable<any>'

我使用了"angular2 webpack""angular2/form,Observable",但是遇到了错误,需要帮助..

有一个自定义表单验证器 --

import {Observable} from 'rxjs/Rx';
import {REACTIVE_FORM_DIRECTIVES,FormControl, FormGroup, Validators} from '@angular/forms';

emailShouldBeUnique(control:FormControl) {
    return new Observable((obs:any)=> {
      control.valueChanges
        .debouceTime(400)
        .distinctUntilChanged()
        .flatMap(term=>return !this.userQuery.emailExist(term))
        .subscribe(res=> {
            if (!res) {obs.next(null)}
            else {obs.next({'emailExist': true}); }; }
        )});}

我可以找到文件 "/projection_direction/node_modules/rxjs/operator/debounceTime.js"

为什么会出现这样的错误--

Property 'debouceTime' does not exist on type 'Observable'.

请确保您已在 main.ts (引导应用程序的位置)

中启动
import "rxjs/add/operator/map";
import "rxjs/add/operator/debounceTime";
...

或一次全部

import "rxjs/Rx";

延长

a working example

//our root app component
import {Component, EventEmitter, ChangeDetectorRef} from '@angular/core'
import {Observable} from  "rxjs/Rx";
@Component({
  selector: 'my-app',
  providers: [],
  template: `
    <div>
      <h2>Hello {{name}}</h2>

        <div>debounced message: {{message}}</div>
    </div>
  `, 
  directives: []
})
export class App {

  protected message: string;
  protected emitter = new EventEmitter<string>();
  public obs: Observable<string>;

  constructor() {
    this.name = 'Angular2 (Release Candidate!)'

    this.obs = this.emitter
      .map(x => x)
      .debounceTime(1200)
      ;

    this.obs.subscribe(msg => this.message = msg);
  }

  ngOnInit(){
    this.emitter.emit("hello after debounce");
  }
}

在 main.ts 我们有:

//main entry point
import {bootstrap} from '@angular/platform-browser-dynamic';
import {App} from './app';

import "rxjs/Rx"; 

bootstrap(App, [])
  .catch(err => console.error(err));

检查一下here

你这里有错字。是 debounceTime,不是 debouceTime :)

我最近在 angular-cli 1.6.8 生成的项目上使用 angular v5.2.6 和 rxjs v5.5.6 时遇到了类似的错误。我原来有:

import { debounceTime, map } from 'rxjs/operators;

因为我订阅了控件 valueChanges 事件,所以我一直收到错误消息,直到我输入

import { Observable } from 'rxjs/Rx';

希望对您有所帮助!

对于 rxjs 6 之后来到这里的每个人:

您现在需要使用 pipe():

什么是

myObservable$
    .debounceTime(500)
    .subscribe(val => {
    // debounced stuff
})

现在需要:

myObservable$
    .pipe(debounceTime(500))
    .subscribe(val => {
    // debounced stuff
})

https://www.learnrxjs.io/operators/filtering/debouncetime.html

对我来说,答案是使用管道:

.pipe(debounceTime(500))

加上更改导入自:

import "rxjs/add/operator/debounceTime"; 

收件人:

import {debounceTime} from 'rxjs/internal/operators';

是的,我正在学习教程,希望这对您有所帮助

我最近遇到了同样的问题,解决后解决了:

import { debounceTime } from 'rxjs/operators';

并且还根据需要添加了管道,我认为 Angular 5+

something.pipe(debounceTime(100)).subscribe(something...);

假设您必须将 debounceTime() 与多个 RxJS 运算符一起使用,我建议使用 .pipe() operator

import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';

 functionName(value: Observable<string>) {
    return .pipe(
      debounceTime(400),
      distinctUntilChanged(),
      switchMap(location => this.secondFunc(value))
    )
  }

2019 年 8 月

一个例子:

进口:

import { Subject } from "rxjs";
import { debounceTime } from "rxjs/operators";

在组件 class 中:

resizeEvent: Subject<any> = new Subject<any>();

@HostListener("window:resize", ["$event"])
onResize(event: any) {
  this.resizeEvent.next(event);
}

ngOnInit() {
  console.log("in ngOnInit");

  this.resizeEvent
    .pipe(debounceTime(500))
    .subscribe(this.actualResizeHandler);
}

actualResizeHandler(event: any) {
  //   event.target.innerWidth;
  console.log("in actualResizeHandler");
}