【问题标题】:ionic 3 HttpClient error in connecting api, Property 'json' does not exist on type 'Object'连接api时出现ionic 3 HttpClient错误,“对象”类型上不存在属性“json”
【发布时间】:2018-10-21 14:02:05
【问题描述】:

我正在使用 Ionic 3,我正在尝试连接到 api,我的代码如下所示。

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/map'

@Injectable()
export class WeatherProvider {
  apikey = 'xxxxxxxxxxxxxxx';
  url;
  constructor(public http: HttpClient) {
    console.log('Hello WeatherProvider Provider');
    this.url= 'http://api.wunderground.com/api/'+this.apikey+'/conditions/q';
  }
  getWeather(city, state){
    return this.http.get(this.url+'/'+state+'/'+city+'.json')
      .map(res => res.json());
  }
}

我知道错误,就是这段代码 .map(res => res.json());

这是错误日志

Typescript 错误属性“json”在类型“Object”上不存在。

src/providers/weather/weather.ts
  return this.http.get(this.url+'/'+state+'/'+city+'.json')
    .map(res => res.json());
}

Ionic 框架:3.9.2 Ionic App 脚本:3.1.9 Angular Core:5.2.10 Angular 编译器 CLI:5.2.10 节点:9.2.1 操作系统平台:Linux 4.13 导航平台:Linux x86_64 用户代理:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, 像 Gecko) Chrome/63.0.3239.132 Safari/537.36

请给我一个解决方案

【问题讨论】:

    标签: html angular ionic3


    【解决方案1】:

    您无需使用 httpClient 调用 .json(),因为默认情况下响应为 JSON

    return this.http.get(this.url+'/'+state+'/'+city+'.json')
        .map(res => res);
    

    【讨论】:

    • 谢谢我的问题现在已经解决了。让我们继续前进,希望它不会为此造成更多问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-16
    • 2018-06-14
    • 2021-10-26
    • 2020-10-18
    • 1970-01-01
    • 2018-02-10
    • 2020-08-07
    相关资源
    最近更新 更多