【问题标题】:angular and http does not understand json - Property 'json' does not exist on type 'Object' [duplicate]角度和http不理解json-“对象”类型上不存在属性“json”[重复]
【发布时间】:2019-06-20 17:37:10
【问题描述】:

我刚刚将我的 Angular 升级到 v7。

这是如何发生的:

const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type':  'application/json',
        'Authorization': 'Bearer ' + localStorage.getItem('token')
      })
};

var url = this.getEndpointUrl(endpoint);

return this.http.get(url, httpOptions).pipe(map(res => res.json()));

得到错误:

[ts] 类型“对象”上不存在属性“json”。 [2339]
任何

我错过了什么?

【问题讨论】:

标签: angular


【解决方案1】:

最新的HttpClient 自动将响应解析为json,所以我们不再需要调用json() 方法。

应该够了

var url = this.getEndpointUrl(endpoint);
return this.http.get(url, httpOptions);

参考: https://angular.io/guide/http#getting-json-data

【讨论】:

    猜你喜欢
    • 2018-02-10
    • 2021-04-11
    • 1970-01-01
    • 2021-09-19
    • 2019-01-09
    • 2018-07-16
    • 2018-05-27
    • 2016-02-28
    • 1970-01-01
    相关资源
    最近更新 更多