Angular 5、rxjs映射到json,'json'类型上不存在对象
Angular 5, rxjs map to json, 'json' does not exist on the the type 'object
尝试关注在线视频,然后出现这个,我是新手 angular,其他解决方案对我没有帮助。
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() );
}
}
使用 angular 5 和 httpClient
,您不再需要使用地图部分。
在此处阅读更多内容:https://angular.io/guide/http#type-checking-the-response
getWeather(city,state){
return this.http.get(this.url+'/'+state+'/'+city+'.json');
}
如果你想获取特定格式的数据,你可以告诉 HttpClient 响应的类型,使输出的消费更容易和更明显。
export interface Config {
heroesUrl: string;
textfile: string;
}
然后:
getConfig() {
// now returns an Observable of Config
return this.http.get<Config>(this.configUrl);
}
如果您使用的是新的 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.
尝试关注在线视频,然后出现这个,我是新手 angular,其他解决方案对我没有帮助。
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() );
}
}
使用 angular 5 和 httpClient
,您不再需要使用地图部分。
在此处阅读更多内容:https://angular.io/guide/http#type-checking-the-response
getWeather(city,state){
return this.http.get(this.url+'/'+state+'/'+city+'.json');
}
如果你想获取特定格式的数据,你可以告诉 HttpClient 响应的类型,使输出的消费更容易和更明显。
export interface Config {
heroesUrl: string;
textfile: string;
}
然后:
getConfig() {
// now returns an Observable of Config
return this.http.get<Config>(this.configUrl);
}
如果您使用的是新的 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.