我试图从Angular调用一个API,但得到这个错误:

属性“map”在类型“Observable<Response>”上不存在

这个类似问题的答案并没有解决我的问题:Angular 2 beta版。17:属性“map”在类型“Observable<Response>”上不存在。

我使用的是Angular 2.0.0-beta.17。


当前回答

对于Angular 7v

改变

import 'rxjs/add/operator/map';

To

import { map } from "rxjs/operators"; 

And

 return this.http.get('http://localhost/ionicapis/public/api/products')
 .pipe(map(res => res.json()));

其他回答

安装rxjs-compat只需在terminal中输入:

npm install --save rxjs-compat

然后导入:

import 'rxjs/Rx';

在rxjs 6中,map操作符的用法已经改变 现在你需要像这样使用它。

import { map } from 'rxjs/operators';
myObservable
  .pipe(map(data => data * 2))
  .subscribe(...);

供参考 https://www.academind.com/learn/javascript/rxjs-6-what-changed/#operators-update-path

在最新的Angular 7中。,你可以简单地尝试:

import { Observable, of } from "rxjs";
import { map, catchError } from "rxjs/operators";

然后你可以使用下面这些方法:

   private getHtml(): Observable<any> {
    return this.http.get("../../assets/test-data/preview.html").pipe(
      map((res: any) => res.json()),
      catchError(<T>(error: any, result?: T) => {
        console.log(error);
        return of(result as T);
      })
    );
  }

查看这个演示以获得更多细节。

我在Angular 2.0.1中也遇到了同样的问题,因为我从

import { Observable } from 'rxjs/Observable';

我解决了从这个路径导入Observable的问题

import { Observable } from 'rxjs';

在Angular v10中。X和RXJS v6.x

第一次导入我的服务地图顶部,

Import {map} from 'rxjs/operators';

然后我用这样的地图

return this.http.get<return type>(URL)
  .pipe(map(x => {
    // here return your pattern
    return x.foo;
  }));