我有一个接口Weather
。我正在尝试为组件内的接口创建一个对象数组。下面的代码是我的接口:Angular - 即使初始化后也无法读取未定义的属性'push'
export interface Weather {
city: String;
temp: String;
description: String;
}
现在我想创建我的组件内部对象的数组,这里是我的组件文件:
export class AppComponent {
title = 'Ng-Weather';
weather: Weather[] = [];
constructor(private weatherService: WeatherService) { }
search(cityName) {
this.weatherService.getWeatherbyName(cityName)
.subscribe(this.storeData);
}
storeData(data: Weather) {
this.weather.push(data);
console.log('weather: ' + this.weather);
}
}
我我的控制台上得到的错误是: ERROR TypeError: Cannot read property 'push' of undefined
谢谢!很好的解释来帮助理解。 – JackSlayer94