简体   繁体   English

具有字符串数组参数的Angular 5 / typescript类构造函数

[英]Angular 5 / typescript class constructor with string array argument

When trying to construct a class by passing in an array of strings, the new object string array is empty. 尝试通过传入字符串数组来构造类时,新的对象字符串数组为空。

I've tried several different approaches to initialize the new objects string array in the constructor, including, but not limited to the following: 我已经尝试了几种不同的方法来初始化构造函数中的新对象字符串数组,包括但不限于以下内容:

this.newArray = oldarray.slice();

this.newArray = Array.from(oldarray);

this.newArray = oldarray.map( s => { return s; });

iterating through the old array and pushing the elements one at a time in to the new array. 迭代旧数组并将元素一次推入新数组。

What is interesting here is that the when creating the for loop, I noticed that the string array acts like it has a property called "array". 这里有趣的是,在创建for循环时,我注意到字符串数组就像它有一个名为“array”的属性。 So the for loop wouldn't process because oldarray.length = 0 , but oldarray.array.length = 5 所以for循环不会处理因为oldarray.length = 0 ,但是oldarray.array.length = 5

I'm able to see this in the debugger. 我能在调试器中看到这个。 If I attempt to change the code to reference oldarray.array , it underlines and complains about the .array portion and fails to compile under ng serve . 如果我尝试更改代码以引用oldarray.array ,它会强调并抱怨.array部分并且无法在ng serve下编译。

I know this is probably going to end up being something easy / stupid, and its just a matter of me staring at the same code for several hours. 我知道这可能最终变得容易/愚蠢,而这只是我几个小时盯着相同代码的问题。

export class Widget {
  public role: string;
  public array: WidgetTuple[];

  constructor(role: string, tuplearray?: WidgetTuple[]) {
    this.role = role;
    this.array = [];
    if (tuplearray != undefined) {
      for (var lcv = 0; lcv < tuplearray.length; lcv++) {
        var tupl = new WidgetTuple(tuplearray[lcv].widgetGrouper);
        this.array.push(tupl);        
      }
    }
  }
}

export class WidgetTuple {
  public widgetGrouper: string[];

  constructor(strvalues?: string[]) {
    this.widgetGrouper = [];
    if (strvalues != undefined) {
      this.widgetGrouper = strvalues.slice();
      //this.widgetGrouper = strvalues.map()
      //for (var lcv = 0; lcv < strvalues.length; lcv++) {
      //  var entry = strvalues[lcv].toString();
      //  this.widgetGrouper.push(entry);
      //}
    }
  }
}

I expect the string elements from the original widgetGrouper string array to be included in the new WidgetTuple object that is added to the Widget array. 我希望原始widgetGrouper字符串数组中的字符串元素包含在添加到Widget数组的新WidgetTuple对象中。

All attempts so far, yield empty string array results in the newly created object. 到目前为止的所有尝试,产生空字符串数组导致新创建的对象。

Thank you in advance for your assistance. 提前感谢您的协助。

This didn't format well in my comment, so here it is, hitting the debug breakpoint in VS and looking at strvalues: 这在我的评论中没有很好地格式化,所以在这里,在VS中调试调试断点并查看strvalues:

-       strvalues   Array(0) [] Object
+       array   Array(4) ["widgetone", "widgettwo", "someitem", …]  Object
        length  0   number
+       __proto__   Array(0) [, …]  Object

Figured it out. 弄清楚了。 I would delete the question if I could, but who knows... this might help somebody else some day. 如果可以的话,我会删除这个问题,但谁知道......这可能有一天会帮助其他人。

Basically, the answer lies within the danger of any . 基本上,答案在于任何危险。

In the component view, I had an input of type any for the widget object. 在组件视图中,我为widget对象输入了any类型。 The object was then incorrectly referenced during its use within the view (adding the array property). 在视图中使用该对象时,该对象被错误地引用(添加数组属性)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM