类型“ Subject <string>”上不存在属性“ map”

时间:2018-12-12 08:20:57

标签: angular

我有两个项目-一个在角度5上都可以工作,另一个在角度6上。

我的代码:

public keyUp = new Subject<string>();
const observable = this.keyUp
      .map(value => (event.target as HTMLInputElement).value)
      .flatMap((search) => {
        return Observable.of(search).delay(300);
      })
      .subscribe((data) => {
        this.filter = data;
      });

错误:

Property 'map' does not exist on type 'Subject<string>',
Property 'of' does not exist on type 'Observable'

我的进口商品:

import { Observable, Subject } from 'rxjs';

我尝试了各种导入,但是结果是相同的。

1 个答案:

答案 0 :(得分:3)

由于这在Angular 6中不起作用,因此可以安全地假设您使用的Rxjs版本为5.5或更高版本。

在这种情况下,以下语法应适用:

import { map, flatMap, delay, debounceTime, distinctUntilChanged } from 'rxjs/operators';
import { of, Observable, Subject } from 'rxjs';

...

public keyUp = new Subject < string > ();
const observable = this.keyUp
  .pipe(
     map(value => (event.target as HTMLInputElement).value),
     debounceTime(500),
     distinctUntilChanged(),
     flatMap((search) => {
       return of(search).pipe(delay(300));
     })
  )
  .subscribe((data) => {
    this.filter = data;
  });
相关问题