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

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

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

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


当前回答

我也有同样的问题,我使用的是Visual studio 2015,它有一个旧版本的typescript。

升级扩展后,问题得到解决。

下载链接

其他回答

在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

感谢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)
 })

使用管道函数中的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;
  })
})

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

这里推荐

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

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

但你可以这样做:

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