【发布时间】:2016-08-08 15:04:35
【问题描述】:
如何初始化一个空数组?
test: any[];
//On change I want to add an item to an array
test(){
this.test.push('a');
}
错误 TS2339:类型 '() => void' 上不存在属性 'push'。
【问题讨论】:
标签: angular typescript
如何初始化一个空数组?
test: any[];
//On change I want to add an item to an array
test(){
this.test.push('a');
}
错误 TS2339:类型 '() => void' 上不存在属性 'push'。
【问题讨论】:
标签: angular typescript
你没有初始化你的数组,所以test 是undefined。为了能够使用它,只需这样初始化它:
test: any[] = [];
【讨论】:
尝试以下任一解决方案:
const test = [];
test.push('a');
或
const test = [];
const name = 'a';
test.push({ 'name' : name});
您可以将其添加到您的函数中,例如:
test(name: string){
const test = [];
test.push({ 'name' : name});
}
【讨论】: