如何将数据推入数组
How to push data into the array
我正在尝试在单击按钮时推送新标题。我有大约 5 个按钮。每次点击我都会将不同的消息推送到数组中。我正在尝试以下代码:
public skills: any[];
public firstMsg: string = 'Press Play to start playing';
onPlay(){
this.skills.push({ title: this.firstMsg })
}
但现在我收到错误:
ERROR TypeError: Cannot read property 'push' of undefined
我想不出我做错了什么。
初始化数组
public skills: any[] = []
声明时需要赋空数组
public skills: any[] = [];
或
public skills = [];
因为javascript不允许对未初始化为数组的变量使用数组属性。
我正在尝试在单击按钮时推送新标题。我有大约 5 个按钮。每次点击我都会将不同的消息推送到数组中。我正在尝试以下代码:
public skills: any[];
public firstMsg: string = 'Press Play to start playing';
onPlay(){
this.skills.push({ title: this.firstMsg })
}
但现在我收到错误:
ERROR TypeError: Cannot read property 'push' of undefined
我想不出我做错了什么。
初始化数组
public skills: any[] = []
声明时需要赋空数组
public skills: any[] = [];
或
public skills = [];
因为javascript不允许对未初始化为数组的变量使用数组属性。