[Vue]"TypeError: Cannot read property '0' of undefined"的解决方法

出现这个错误的原因其实是Vue在拿到数据之前渲染了dom

比如
{{dataParent.dataSons[0]}}
这种,当请求不到数据时就会报这种错误

那么,解决方案是啥呢,是啥呢,是啥呢?

{{dataParent.dataSons[0]}}

这样就好啦

还有一种解决方案,如果在mounted里触发的获取数据方法

可以试试用setTimeout包裹一下

mounted(){
  setTimeout(
    () => {
        ...
   },0)
}

你可能感兴趣的:([Vue]"TypeError: Cannot read property '0' of undefined"的解决方法)