TypeError:search.valueChanges.debounceTime不是函数

use*_*653 51 rxjs typescript angular2-forms angular

我只是在学习angular2.在输入更改时应用某些内容时,我收到错误.

app.ts:

export class AppComponent {
    form: ControlGroup;

    constructor(fb: FormBuilder) {
        this.form = fb.group({
            search: []
        });

        var search = this.form.find('search');
        search.valueChanges
            .debounceTime(400)
            .map(str => (<string>str).replace(' ','?'))
            .subscribe(x => console.log(x));
    };

 }
Run Code Online (Sandbox Code Playgroud)

错误:

在此输入图像描述

怎么解决这个?我错过了什么吗?

Plunker演示

NB我现在无法在plunker上制作任何东西,因为我第一次在plunker上写了angular2.我在plunker只写了我的app.ts代码.我已经在本地电脑上显示了错误的屏幕截图.如果你告诉我在plunker上运行angular2项目的方式,我将不胜感激.

Par*_*ain 90

你只需要导入这些来删除你的错误.您收到运行时错误,因为默认情况下Observables只有几个运算符.你必须像这样明确地导入它们 -

import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/map';
Run Code Online (Sandbox Code Playgroud)

Working example

  • 我正在使用“ rxjs / operator / debounceTime”中的“ import {debounceTime}”导入它,但无法正常工作。 (2认同)
  • 您需要像这样从'rxjs / operators'导入角度6的{debounceTime}的debounceTime(); (2认同)
  • 从'rxjs / operators'导入{debounceTime}; (2认同)

小智 6

Put debounceTime(400) inside a pipe() function.

example

var search = this.form.find('search');
    search.valueChanges
        .pipe(debounceTime(400))
        .map(str => (<string>str).replace(' ','?'))
        .subscribe(x => console.log(x));
Run Code Online (Sandbox Code Playgroud)


Adr*_*ian 5

两件事情:

为每个运算符添加导入

import { debounceTime, distinctUntilChanged } from 'rxjs/operators';
Run Code Online (Sandbox Code Playgroud)

然后使用管道传递所有 RxJS 运算符

    this.searchTextInput.valueChanges
      .pipe(
        debounceTime(500),
        distinctUntilChanged(),
      )
      .subscribe((term): void => {
        console.log({ term });
      });
Run Code Online (Sandbox Code Playgroud)