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

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

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

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


当前回答

只需在项目的VS Code终端中写入此命令并重新启动项目。

npm install rxjs-compat

你需要通过添加这个来导入map操作符:

import 'rxjs/add/operator/map';

其他回答

再次讨论这个问题,因为我的解决方案没有列在这里。

我正在用rxjs 6.0运行Angular 6,遇到了这个错误。

下面是我解决问题的方法:

我改变了

map((response: any) => response.json())

简单地成为:

.pipe(map((response: any) => response.json()));

我在这里找到了解决方法:

https://github.com/angular/angular/issues/15548#issuecomment-387009186

angular的新版本不支持.map,你必须通过cmd安装 保存rxjs-compat 通过这个你可以享受老技术。 注意: 不要忘记导入这些行。

import { Observable, Subject } from 'rxjs';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

角9:

 forkJoin([
  this.http.get().pipe(
    catchError((error) => {
      return of([]);
    })
  ),
  this.http.get().pipe(
    catchError((error) => {
      return of([]);
    })
  ),

只需运行NPM install—save rxjs-compat即可修复错误。

这里推荐

我也曾犯过同样的错误。一个对我有效的解决方案是在您的服务中添加以下行。Ts文件而不是导入'rxjs/add/operator/map':

import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

举个例子,我的app。service。ts在调试之后,

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class AppService {
    constructor(private http: HttpClient) {}
    getData(): Observable<any> {
        return this.http.get('https://samples.openweathermap.org/data/2.5/history/city?q=Warren,OH&appid=b6907d289e10d714a6e88b30761fae22')
        .pipe(map(result => result));
    }
}