【发布时间】:2017-04-27 19:32:08
【问题描述】:
我有两个界面如下:
export interface ICard
{
cardNumber:string;
cardAddress: string;
city: string;
}
export interface IStudent
{
cards: ICard[];
name:string;
}
在我的 typescript(.ts) 文件中,我已经在我的班级下面初始化了
private student: IStudent = <IStudent>{}; // Initializing my interface;
private cardDetails: ICard[]; // Unable to use the same initialization like above
在我的函数中,下面我试图将多张卡片添加到我的 cardDetails 并将 cardDetails 分配给我的 IStudent 界面。
loadStudentCards()
{
var card: ICard = <ICard>{}// Initializing here.
card.cardNumber = "12345";
card.cardAddress = "Some address";
card.city = "Seattle";
this.cardDetails.push(card); // This is the place i am seeing error "Cannot read property 'push' of undefined"
this.student.cards = this.cardDetails;
}
这是我看到错误“无法读取未定义的属性'push'”的地方
这可能是一个简单的问题,但不确定如何在此处初始化接口数组对象。有人可以在这里提示一下吗?
【问题讨论】:
标签: angular typescript interface