我有这样的代码问题
最佳做法是什么?谢谢
错误:TypeError:无法读取未定义的属性“进程”
myComponent.ts
ProcessInfo: any | false;
showSaveItems = () => {
this.typeCategory = [];
this.dataTypesLoop = [];
if (this.ProcessInfo.process.data.mortgage.length) {
for (let i = 0; i < this.ProcessInfo.process.data.mortgage.length; i++) {
if (this.ProcessInfo.process.data.mortgage[i].mortgageHeader !== null) {
const findItem = this.categories.find(
obj =>
obj.value ==
this.ProcessInfo.process.data.mortgage[i].mortgageHeader.category
);
}
}
}
this.dataTypes = new Set(this.dataTypesLoop);
return new Set(this.typeCategory);
};
myComponent.html
<li *ngFor="let item of showSaveItems()">
<strong>{{ item.text }}</strong>
</li>
您应检查路径是否存在
if (this.ProcessInfo && this.ProcessInfo.process && this.ProcessInfo.process.data && this.ProcessInfo.process.data.mortgage && this.ProcessInfo.process.data.mortgage.length>0) {
您需要在使用之前实例化您的参数:
ProcessInfo: any | false = false;
使用':'只是设置他的类型,但是在使用'='之前他的值是'undefined';