首页 > 解决方案 > “主题”类型上不存在属性“地图”'

问题描述

我有两个项目 - 一个在 angular 5 上 - 一切正常,还有 angular 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';

我尝试了各种导入,但结果是一样的..

标签: angular

解决方案


由于这在 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;
  });

推荐阅读