类型"Observable< Response>"上不存在属性"map" [英] Property 'map' does not exist on type 'Observable<Response>'

查看:168
本文介绍了类型"Observable< Response>"上不存在属性"map"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Angular调用API,但出现此错误:

I am trying to call an API from Angular but am getting this error:

Property 'map' does not exist on type 'Observable<Response>'

这个类似问题的答案不能解决我的问题:上不存在属性"map".

The answers from this similar question didn't solve my issue: Angular 2 beta.17: Property 'map' does not exist on type 'Observable<Response>'.

我正在使用Angular 2.0.0-beta.17.

I am using Angular 2.0.0-beta.17.

推荐答案

您需要导入map运算符:

import 'rxjs/add/operator/map'

或更笼统地说:

import 'rxjs/Rx';


注意:对于RxJS 6.x.x及更高版本,您将必须使用可管道运算符,如以下代码片段所示:


Notice: For versions of RxJS 6.x.x and above, you will have to use pipeable operators as shown in the code snippet below:

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更改日志中的重大更改.更多信息.

This is caused by the RxJS team removing support for using See the breaking changes in RxJS' changelog for more info.

来自更改日志:

运算符:现在必须从rxjs导入可管道运算符,如下所示:import { map, filter, switchMap } from 'rxjs/operators';.没有深度导入.

operators: Pipeable operators must now be imported from rxjs like so: import { map, filter, switchMap } from 'rxjs/operators';. No deep imports.

这篇关于类型"Observable&lt; Response&gt;"上不存在属性"map"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆