尝试关注在线视频,然后出现这个,我是 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() );
}
}
最佳答案
如果您使用新的 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.
关于json - Angular 5,rxjs 映射到 json,类型“对象”上不存在 'json',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49884348/