【vue报错】vue.esm.js?c9fd:628 [Vue warn]: Error in render: “TypeError: Cannot read

169 阅读1分钟

Vue里面的一个坑:vue.esm.js?c9fd:628 [Vue warn]: Error in render: “TypeError: Cannot read properties of undefined (reading ‘0’)”;

在页面使用到的数据是嵌套多层的时候,比如是拿的是数组的第三层的数据进行展示,像data[0][0],在页面进行初始化的时候,会报错:Cannot read properties of undefined (reading ‘0’);但是页面是可以正常使用的。

原因

需要显示的数据是通过异步方法拿到的,当拿到后台的数据时,vue的声明周期已经从beforeCreate走到了mounted,所以一开始在初始化时数据是为空的,报错则是因为在显示初始数据的时候报错

解决方法

避免在没有数据的时候显示解析,有数据的时候元素才存在,用v-if控制