我正在尝试编码一个数组,该数组应该包含一些项目的对象,但是我总是会得到一个errormessage,称为:" this.allgmarray未定义",但实际上不是:
allgmArray: DialogBook[];
[...]
load(){
for(var i = 0; i < this.service.allgmTempArray.length; i++) {
if(this.service.allgmTempArray[i].type == this.service.tempGroup.get('type').value){
var fullName = this.service.tempGroup.get('fullName').value;
console.log(fullName) //works fine
var type = this.service.allgmTempArray[i].type;
console.log(type) //works fine
var device = this.service.allgmTempArray[i].device;
console.log(device) //works fine
var date_from = this.service.tempGroup.get('date_from').value;
console.log(date_from) //works fine
var date_to = this.service.tempGroup.get('date_to').value;
console.log(date_to) //works fine
var date_from_og = this.service.tempGroup.get('date_from_og').value;
console.log(date_from_og) //works fine
var date_to_og = this.service.tempGroup.get('date_to_og').value;
console.log(date_to_og) //works fine
var obj = {
fullName: fullName,
type: type,
device: device,
date_from: date_from,
date_to: date_to,
date_from_og: date_from_og,
date_to_og: date_to_og
}
console.log(obj) //<- Comes out the right obj
this.allgmArray.push(obj) //<- I think heres the problem
}
}
console.log(this.allgmArray) //Error: this.allgmArray is undefined
//And "DialogBook" interface look like:
export interface DialogBook {
fullName: String;
device: String;
type: String;
date_from: String;
date_to: String;
date_from_og: Date;
date_to_og: Date;
}
所以我真的不知道问题是什么,需要一些帮助
allgmArray: DialogBook[];
是一个定义,而不是一个实例。
allgmArray: DialogBook[] = [];
应该解决您的问题。