【发布时间】:2020-06-12 15:31:41
【问题描述】:
我正在尝试初始化 Angular6 服务中的一些对象,但我收到一个错误,说明我的私有属性之一未定义。
起初我尝试过:
private mockIncident: Incident[];
constructor() {
this.mockIncidentRaw.forEach(incident => {
this.mockIncident.push(new Incident().deserialize(incident))
});
}
但我收到错误消息,提示未定义 mockIncident。
ERROR 错误:未捕获(承诺中):TypeError:无法读取未定义的属性“push”。
然后尝试了:
public mockIncident: Incident[];
constructor() {
init();
}
init = () => {
for(let i = 0; this.mockIncidentRaw.length; i++) {
this.mockIncident.push(new Incident().deserialize(this.mockIncidentRaw[i]))
}
}
【问题讨论】:
-
你从来没有给它一个值,只是一个类型,为什么这令人惊讶?
-
另外,
this.mockIncidentRaw是怎么初始化的?我想你的问题还不止我的回复。 -
@jonrsharpe this.mockIncidentRaw 包含该值,但此处不相关
-
@Exomus 唯一的问题是您在答案的第二部分中提到的数组没有初始化
-
你是对的,这不相关,这不是你想要推动的。
标签: javascript angular angular-services