解决element-ui中集合使用 size 报错问题

1. 问题描述

最近写前后端分离项目中 遇到需获取到后端返回对象的集合属性值的大小的场景
一开始使用如下的解决方法,直接使用集合的 size 属性:

<el-table-column label="操作" width="100" align="center" fixed="right">

    <template slot-scope="scope">

        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"

                        :disabled="scope.row.childList.size != 0"/>

    </template>

</el-table-column>

 但是运行时候发现页面无法正常显示 页面调试后报错如下:

2. 问题分析 

显然前端无法解析当前 size 属性值,出现这个问题的原因是在我们使用集合时没有事先进行非空判断,出现 null.size 的错误。
为了保险起见还是再使用 size 属性前先使用 childList != undefined 判断非空。

3. 解决方案

<el-table-column label="操作" width="100" align="center" fixed="right">

    <template slot-scope="scope">

        <el-button type="danger" icon="el-icon-delete" size="mini" @click="removeDataById(scope.row.id)" title="删除"

                   :disabled="scope.row.childList != undefined && scope.row.childList.size != 0"/>

    </template>

</el-table-column>

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值