vue获取数组中对数组长度判断报错


<p>
<router-link :to="'/detailhospital/'+branch.id" v-for="(branch,index) in data.withhospitals" v-show="index<3">{{branch.name}}
</router-link>
</p>
<p v-if="data.withhospitals.length>3">
<router-link :to="'/detailhospital/'+branch.id" v-for="(branch,index) in data.withhospitals" v-show="index>3">{{branch.name}}</router-link>
</p>
data.withhospitals.length这样对数据数组长度进行判断会报以下错误:
TypeError: Cannot read property 'length' of undefined"
只要进行对数据是否存在进行判断,就解决了数组长度报错了
<p v-if="data.withhospitals!== undefined && data.withhospitals!== null&&data.withhospitals.length>3">
<router-link :to="'/detailhospital/'+branch.id" v-for="(branch,index) in data.withhospitals" v-show="index>3">{{branch.name}}</router-link>
</p>
 
 
 

0 个评论

要回复文章请先登录注册