私は小さな天気アプリを持っています。すべてのブラウザで正常に動作しますが、私はTSコンパイラからエラーを取得しています:プロパティname
、main
、wind
タイプには存在しませんWeather[]
。しかし、私はこれらのプロパティをクラスに追加したようWeather[]
です...
export class AppComponent implements OnInit {
weather:Weather[];
temp:string;
pressure:number;
humidity:number;
wind_speed:number;
wind_dec:number;
city:string;
activeId:string = "Misto Kyyiv";
constructor(private getWeatherService:GetWeatherService) {}
ngOnInit() {
this.getWeatherService.getWeather("Misto Kyyiv").subscribe(weather => {
this.weather = weather;
this.temp = (weather.main.temp -273.15).toFixed(2);
this.pressure = weather.main.pressure;
this.humidity = weather.main.humidity;
this.wind_speed = weather.wind.speed;
this.wind_dec = weather.wind.deg;
this.city = weather.name;
console.log(this.weather);
console.log(this.temp);
});
}
export class Weather {
main:any = {};
wind:any = {};
temp:number;
pressure:number;
humidity:number;
wind_speed:number;
wind_dec:number;
city:string;
activeId:string;
name:string;
}
//Get Weather
//it worked when I changed here Weather[] to Weather !!!
getWeather(city:string):Observable<Weather> {
let key = "c2dcf8ffb5cdc3f8977bfd2ae7ea4738";
let url = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "&?units=metric&APPID=" + key;
return this.http.get<Weather>(url);
}
に変更Weather[]
するWeather
と、TSコンパイラが怒鳴り止みました!
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加