我有以下代码
export class FormComponent implements OnInit {
name: string;
empoloyeeID : number;
empList: Array<{name: string, empoloyeeID: number}> = [];
constructor() {
}
ngOnInit() {
}
onEmpCreate(){
console.log(this.name,this.empoloyeeID);
this.empList.push.apply(this.name,this.empoloyeeID);
this.name ="";
this.empoloyeeID = 0;
}
}
但是这个抛出错误
CreateListFromArrayLike 在非对象上调用
还有什么方法可以创建自定义类和使用的对象列表,而不是在这里定义数组。
谢谢
原文由 Md. Parvez Alam 发布,翻译遵循 CC BY-SA 4.0 许可协议
是的,有办法做到这一点。
首先声明一个类。
然后在您的组件中导入该类
另一种方法是 接口 阅读文档一次 - https://www.typescriptlang.org/docs/handbook/interfaces.html
还要检查这个问题,这很有趣 -When to use Interface and Model in TypeScript / Angular2