报错
说明 findVM 方法里,this.vms[i] 是 undefined,导致 this.vms[i].vm 报错。
1. 问题根源分析
crud.js 相关代码大致如下
findVM(type) {
for (let i = 0; i < this.vms.length; i++) {
if (this.vms[i].type === type) {
return this.vms[i].vm;
}
}
return undefined;
}
如果 this.vms[i] 是 undefined,this.vms[i].type 就会报错。
2. 如何修复?
只需要在 findVM 方法里加一层健壮性判断:
findVM(type) {
for (let i = 0; i < this.vms.length; i++) {
if (this.vms[i] && this.vms[i].type === type) {
return this.vms[i].vm;
}
}
return undefined;
}
这样就不会再报 Cannot read properties of undefined (reading 'vm') 了。
3. 建议你这样改
请在 src/components/Crud/crud.js 的 findVM 方法里加上 this.vms[i] && 的判断。即可