“Error in render: "TypeError: Cannot read property '0' of undefined"”错误问题

160 阅读1分钟

错误截图

image.png 原因是模板在渲染时候未读取到这个0

image.png

这里的 skuImageList 是 vuex 中 state 的数据,

在 vue 渲染机制中异步数据先显示初始数据,再显示带数据的数据,初始数据为空不存在【0】。

这就是报错了页面仍然正常运行的原因。

所以解决方法就显而易见,在 img 中加入 v-if 让数据还未加载时不显示即可。

<img v-if="skuImageList[0]" :src="skuImageList[0].imgUrl" />