【发布时间】:2019-08-28 10:35:33
【问题描述】:
我有一个ProductButton.ts 课程
export class ProductButton{
public ProductBtnID: string;
public ProductID: string;
public ParentWfTaskID: string;
public WfTaskID: string;
public TaskName: string;
public ButtonText: string;
public ImageContentID: number;
}
我正在尝试将其导入我的组件并创建它的新实例。 但我似乎无法为其创建新实例。
这是我的组件类
import { ProductButton } from '../../models/ProductButton';
@Component({
templateUrl: './product-edit.component.html',
providers: []
})
export class ProductEditComponent implements OnInit {
constructor(){}
addChildTask(){
if(!this.product.ProductButtons)
this.product.ProductButtons = [];
var pButton = new ProductButton();
pButton.ButtonText = "";
this.product.ProductButtons.push(pButton);
}
}
为什么我尝试新建ProductButton 的实例我得到一个没有属性的空对象。每次点击该方法时如何创建一个新实例?
【问题讨论】:
-
您不提供任何这些属性,没有构造函数来初始化它们,并且类型仅在编译时存在。你期望的输出是什么?
-
@jonrsharpe 你说得对,我对 TS 不是很熟悉。我没有想到像 JS 类一样初始化它。我在网上看不到很多例子。
标签: html angular typescript components