【问题标题】:Error : .map() is not a function [duplicate]错误:.map() 不是函数 [重复]
【发布时间】:2016-05-24 11:42:48
【问题描述】:

尝试订阅已解析的 HTTP 响应 - 来自组件的 GET。得到一个错误,说 .map 不是从 HTTPService 或组件类使用的函数。

Httpdemo.getResponse().map(res => res.json()).subscribe((cities)=>{ 
   this.cities = cities.cities;          
   console.log(this.cities);
});

但以下工作:

Httpdemo.getResponse().subscribe((cities)=>{ 
    this.cities = JSON.parse(cities._body).cities;          
    console.log(this.cities);
});

解析在 Httpdemo 服务中也不起作用。

return this.http.get('./app/cities.json').map(res => res.json())

我的代码中这里的用法有什么问题?

【问题讨论】:

    标签: http xmlhttprequest angular


    【解决方案1】:

    您需要为 Observable 导入 map 操作符,如下所述:

    import 'rxjs/add/operator/map';
    

    默认情况下,并非所有运算符都包括在内。

    查看此答案了解更多详情:

    【讨论】:

      【解决方案2】:

      一般来说,如果您对加载/定位单个 Rxjs 运算符不感兴趣,我建议您导入下面的库。

      使用,

      导入 'rxjs/Rx';

      【讨论】:

        猜你喜欢
        • 2017-08-16
        • 2020-02-28
        • 1970-01-01
        • 2019-12-18
        • 2020-10-18
        • 1970-01-01
        • 1970-01-01
        • 2020-09-20
        • 2017-04-28
        相关资源
        最近更新 更多