[英]Angular 5, rxjs map to json, 'json' does not exist on the the type 'object
Trying to follow an online video, then this appears, I am new to angular, other solutions are not helping me out. 尝试观看在线视频,然后出现,这对我是陌生的,其他解决方案也无济于事。
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() );
}
}
If you're using the new HttpClient
you don't need to parse JSON because it's decoded automatically for you: 如果您使用的是新的
HttpClient
,则无需解析JSON,因为它会自动为您解码:
https://angular.io/guide/http#type-checking-the-response https://angular.io/guide/http#type-checking-the-response
The
HttpClient.get()
method parsed the JSON server response into the anonymous Object type.HttpClient.get()
方法将JSON服务器响应解析为匿名Object类型。 It doesn't know what the shape of that object is.它不知道该对象的形状是什么。
Also https://angular.io/guide/http#requesting-non-json-data . 也是https://angular.io/guide/http#requesting-non-json-data 。
With angular 5 and httpClient
, you don't need to use the map part anymore. 使用angular 5和
httpClient
,您不再需要使用map部件。
Read more here: https://angular.io/guide/http#type-checking-the-response 在此处了解更多信息: https : //angular.io/guide/http#type-checking-the-response
getWeather(city,state){
return this.http.get(this.url+'/'+state+'/'+city+'.json');
}
If you want to get data in specific format, You can tell HttpClient the type of the response to make consuming the output easier and more obvious. 如果要获取特定格式的数据,则可以告诉HttpClient响应的类型,以使输出的使用更加容易和明显。
export interface Config {
heroesUrl: string;
textfile: string;
}
And then : 接着 :
getConfig() {
// now returns an Observable of Config
return this.http.get<Config>(this.configUrl);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.