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

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

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

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


当前回答

我使用的是Angular 5.2,当我使用import 'rxjs/Rx';它抛出我以下lint错误: TSLint:此导入被列入黑名单,改为导入子模块(import-blacklist)

请看下面的截图:

解决方案: 通过只导入我需要的操作符解决了这个问题。例子如下:

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';

因此,解决办法是只导入必要的操作符。

其他回答

对于所有遇到此问题的Linux用户,检查rxjs-compat文件夹是否被锁定。我有这个完全相同的问题,我进入终端,使用sudo su给整个rxjs-compat文件夹的权限,它是固定的。假设你进口了

import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch'; 

在项目中。原始的.map错误发生在Ts文件中。

你需要导入map操作符:

import 'rxjs/add/operator/map'

或者更一般地说:

import 'rxjs/Rx';

注意:对于RxJS 6.x版本。X和以上,你将必须使用可管道的操作符,如下面的代码片段所示:

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

// ...
export class MyComponent {
  constructor(private http: HttpClient) { }
  getItems() {
    this.http.get('https://example.com/api/items').pipe(map(data => {})).subscribe(result => {
      console.log(result);
    });
  }
}

这是由于RxJS团队删除了对使用的支持 更多信息请参见RxJS更新日志中的重大更改。

更新日志如下:

operator:可管道操作符现在必须像这样从rxjs中导入:import {map, filter, switchMap} from 'rxjs/operators';没有深度进口。

使用管道函数中的map函数,它将解决您的问题。 您可以在这里查看文档。

this.items = this.afs.collection('blalal').snapshotChanges().pipe(map(changes => {
  return changes.map(a => {
    const data = a.payload.doc.data() as Items;
    data.id = a.payload.doc.id;
    return data;
  })
})

我也曾犯过同样的错误。一个对我有效的解决方案是在您的服务中添加以下行。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));
    }
}

在angular的新版本的httpClient模块中,你还没有这样写:

return this.http.request(request)
      .map((res: Response) => res.json());

但你可以这样做:

return this.http.request(request)
             .pipe(
               map((res: any) => res.json())
             );