【问题标题】:Why is my Array undefined but it actually should have an Object?为什么我的数组未定义,但它实际上应该有一个对象?
【发布时间】:2019-05-14 12:08:56
【问题描述】:

我正在尝试编写一个应该包含项目的一些对象的数组,但我总是收到一个错误消息,称为:“this.allgmArray is undefined”但实际上不是:

  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;
}

所以我真的不知道问题是什么,需要一些帮助

【问题讨论】:

  • DialogBookObject 所以这个错误来了。 allgmArray: DialogBook[] = [];试试这个
  • 在定义时尝试初始化 allgmArray。 allgmArray: DialogBook[]=[];

标签: arrays angular undefined push


【解决方案1】:
allgmArray: DialogBook[];

是一个定义,而不是一个实例。

allgmArray: DialogBook[] = [];

应该可以解决您的问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 2012-01-05
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 1970-01-01
    相关资源
    最近更新 更多