问题描述
我想在对象内部创建对象数组。像这样
MyMainObject: {
myArray : [{name:string,available:boolean}]
}
并将值动态推入数组
解决方法
第一个创建界面
export interface MainObject {
myArray: {name: string,available: boolean}[];
}
然后在组件中使用该接口。
export class MyComponent {
MyMainObject: MainObject = {} as MainObject;
constructor() {
this.MyMainObject.myArray.push({ name: 'name',available: true });
console.log(this.MyMainObject);
}
}
,
//Simply declare Object like inside class
MyMainObject: {
myArray : []
};
//nd push value dynamically like inside any method
if(condition){
this.MyMainObject['myArray'].push(
{name:string,available:boolean}
)
}