gpt4 book ai didi

json - Angular 5,rxjs 映射到 json,类型“对象”上不存在 'json'

转载 作者:行者123 更新时间:2023-12-05 08:41:27 25 4
gpt4 key购买 nike

尝试关注在线视频,然后出现这个,我是 Angular 新手,其他解决方案对我没有帮助。

enter image description here

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

/*
Generated class for the WeatherProvider provider.

See https://angular.io/guide/dependency-injection for more info on providers
and Angular DI.
*/
@Injectable()
export class WeatherProvider {
apikey='7d2dc7a226a78c14';
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() );
}
}

最佳答案

如果您使用新的 HttpClient,则不需要解析 JSON,因为它会自动为您解码:

https://angular.io/guide/http#type-checking-the-response

The HttpClient.get() method parsed the JSON server response into the anonymous Object type. It doesn't know what the shape of that object is.

还有 https://angular.io/guide/http#requesting-non-json-data .

关于json - Angular 5,rxjs 映射到 json,类型“对象”上不存在 'json',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884348/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com