我有一個接口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