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

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

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

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


当前回答

首先运行安装,如下所示:

npm install --save rxjs-compat@6

现在你需要在service.ts中导入rxjs:

import 'rxjs/Rx'; 

瞧!问题已经解决了。

其他回答

import { map } from "rxjs/operators";

getGetFunction(){
this.http.get('http://someapi')
.pipe(map(res => res));
}

getPostFunction(yourPara){
this.http.get('http://someapi',yourPara)
.pipe(map(res => res));
}

在上面的函数中,你可以看到我没有使用res.json(),因为我使用HttpClient。它自动应用res.json()并返回Observable (HttpResponse < string>)。在HttpClient中使用angular 4之后,你不再需要自己调用这个函数。

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

这里推荐

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

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

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

感谢https://github.com/nagamallabhanu

https://github.com/webmaxru/pwa-workshop-angular/issues/2#issuecomment-395024755

封装

管(地图(…))

工作

import { map } from 'rxjs/operators';

courseRef: AngularFireList<any>;
courses$: Observable<any[]>;

this.courseRef = db.list('/tags');
  this.courses$ = this.courseRef.snapshotChanges()
  .pipe(map(changes => {
      return changes.map(c => ({ key: c.payload.key, ...c.payload.val() 
  }));
 }));
 this.courses$.subscribe(res=>{
   console.log(res)
 })