不允许我将对象推入数组 [英] Not allowing me to push object into an array
问题描述
我正在使用Angular.我正在尝试将对象推入数组,但是它给了我错误.它说 TypeError:无法读取未定义的属性"push" 那是下面的代码.文章是对象.
I'm working with Angular. I'm trying to push an object into an array but it giving me errors. It says that TypeError: Cannot read property 'push' of undefined That is the code below. Article is an object.
articles: Article[];
addArticle(newTitle: HTMLInputElement, newLink: HTMLInputElement): boolean {
this.articles.push(new Article(newTitle.value, newLink.value, 0));
newTitle.value = '';
newLink.value = '';
return false;
}
我该怎么办?
推荐答案
因此,如果它对您有用,并且我至少看到了一些类似的问题,就可以了.
如果您有TypeError: Cannot read property 'whatever' of undefined
,则表示您未声明某些属性.
这是声明:
So if it worked for you and as I saw at least few similar questions latly.
If you have TypeError: Cannot read property 'whatever' of undefined
that means you didn't declared some property.
This is declaration:
someProperty: SomeType = []
如果只有它的唯一定义:
if you have this its only definition:
someProperty: SomeType
并尝试对其进行任何操作都会导致上述错误.
and trying do any operation on it will result in above error.
这篇关于不允许我将对象推入数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!