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

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

问题描述

我正在尝试从 Angular 调用 API,但出现此错误:

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

属性 'map' 不存在于类型 'Observable'

这个类似问题的答案没有解决我的问题:Angular 2 beta.17:属性 'map' 不存在于类型 'Observable'.

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.

来自变更日志:

operators:现在必须从 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天全站免登陆