TypeScript中的数组数组 [英] Array of Arrays in TypeScript
本文介绍了TypeScript中的数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将对象数组添加到TypeScript中的数组(对于Angular 2应用)。这是我的代码的简化版本:
I'm trying to add an object array to an array in TypeScript (for an Angular 2 app). Here's a stripped down and simplified version of my code:
mylist.ts:
mylist.ts:
export class myList {
constructor(
Number1: number,
String1: string
){}
}
mylist.component.ts:
mylist.component.ts:
import { myList } from './myList';
export class ProductDetailComponent {
myNumber: number;
myString: string;
myList: Array<myList>;
constructor() {
this.myNumber = 10;
this.myString = "some text";
}
addNavigation() {
this.myList = [ new myList(this.myNumber, this.myString) ];
console.log(JSON.stringify(this.myList));
}
}
输出:
[{}]
我在做什么错?
推荐答案
您没有绑定到 MyList
因此您的对象为空。
You're not binding to a property of MyList
thus your object is empty.
将您的类更改为以下内容
Change your class to the following
export class myList {
constructor(
public Number1: number,
public String1: string
){}
}
通过添加 public
或 private
TypeScript将为您创建属性。
现在的结果将是:
By adding public
or private
TypeScript will create properties for you.
Now the result will be:
[{Number1: val, String1: val}]
这篇关于TypeScript中的数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文