报错Cannot read property ‘length’ of undefined
<div>
<el-row class="title2" v-if="dataList.attachmentList.length != 0">
<span>附件列表:</span>
<el-row v-for="item in dataList.attachmentList" :key="item.id">
<a @click="downloadFile(item.attachName,item.attachUrl)">{{item.attachName}}</a>
</el-row>
</el-row>
</div>
- 渲染页面调用
dataList.attachmentList.length
方法时,后台数据还没有返回回来,此时attachmentList为null
,所以会报错。
解决办法:在外层加if判断,这样当数据还没有返回时,页面不会渲染。
代码:
<div v-if="dataList.attachmentList">
<el-row class="title2" v-if="dataList.attachmentList.length != 0">
<span>附件列表:</span>
<el-row v-for="item in dataList.attachmentList" :key="item.id">
<a @click="downloadFile(item.attachName,item.attachUrl)">{{item.attachName}}</a>
</el-row>
</el-row>
</div>