【发布时间】:2020-08-04 16:41:06
【问题描述】:
我有一个这样声明的接口,
export interface OurHistory {
ourHistory?: object;
step1?:object;
step2?:object;
}
在课堂上,我有
export class HistoryComponent implements OnInit, OnDestroy {
myHistory:OurHistory;
let ourHistory = [];
ourHistory = this.allHistory.fields['ourHistory'];
this.myHistory.ourHistory = ourHistory[0];
}
我收到一条错误消息,提示无法设置未定义的属性“ourHistory”
【问题讨论】:
-
因为 myHistory 变量未初始化。默认情况下它是未定义的。
-
要么从实现接口的类中实例化 myHistory 变量,即 myHistory = new Hisory(),要么使用类似 myHistory: OurHistory = {};
标签: angular typescript angular7