angular - 角度,类型上不存在属性
问题描述
我有一个小的天气应用程序。在浏览器中一切正常,但我从 TS 编译器收到错误: property name
, main
, wind
doesn't exist on type 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 编译器停止了大喊大叫!
推荐阅读
- mobile - 如何创建 M-dot 网站?
- php - mysqli_connect(): (HY000/2002): 使用 ipv6 地址拒绝连接
- python - 当我将损失乘以标量时,为什么在训练 Keras 模型时会得到不同的结果?
- php - Sonata 管理员无法加载“Sonata\CoreBundle\Twig\Extension\FlashMessageRuntime”运行时
- dns - 公司域分配 Heroku IP
- c - 使用scanf函数运行for循环的正确方法是什么
- swift - collectionViewCell.xib 中的容器视图在哪里
- node.js - 运行节点命令时出现错误
- ruby - 每次在Ruby中运行脚本时如何增加变量?
- mongodb - 没有时间戳的MongoDB日期范围搜索