【发布时间】:2020-04-24 19:33:34
【问题描述】:
当onSubmit 事件引发时,我试图将一个新值推到数组的末尾。
我的代码如下所示:
accounts: Account[] = [];
feeAccount: Account = new Account();
onSubmit(f: FormsModule) {
console.log('Fee account -> ' + this.feeAccount);
this.accounts.push(this.feeAccount);
}
问题是我收到一个错误:Cannot read property 'push' of undefined.,尽管在它尝试推送之前记录了正确的值。这里有什么问题?我相信如果它以相同的方法正确记录,它就被定义了,还是我出错了?
【问题讨论】:
-
检查您是否无意中改变了
accounts,它似乎在上面的代码中被正确定义,但如果您收到该错误,则表明它无法访问this.accounts,建议它已从空数组更改为未定义。
标签: angular typescript