今天刚使用vue.js,在页面上使用v-if条件进行判断是否显示时,对数组进行长度判断(arr.length)时,报错:

Error in render: "TypeError: Cannot read property 'length' of undefined"

Cannot read property 'length' of undefined

<h1 class="btn_change" v-if="List.tasks.length<1">暂无数据……</h1>

就是判断当数组获取到0条的时候提示用户没有数据。但是如上面的写法,报错了。

解决办法:

<h1 class="btn_change" v-if="List.tasks && List.tasks.length < 1">暂无数据……</h1>

首先我们要判断有List.tasks再对其长度List.tasks.length进行判断,这样就解决了,如果是v-show也是一样的。

Logo

前往低代码交流专区

更多推荐