element ui 表格中,使用length判断报错

11 篇文章 1 订阅

 控制台报这个错的话,先排查html代码中是否使用了xxx.length,代码中是这样写的,

<el-table>  
    <el-table-column label="操作" align="center" width="330">
       <template slot-scope="scope">
          <el-button v-show="scope.row.children.length>0" size="mini"         
             type="primary" plain @click="dowload(scope.row)">
             预览</el-button>
          <el-button v-show="scope.row.children.length>0" size="mini" 
             type="primary" plain @click="dowload(scope.row)">
             下载</el-button>
          <el-button v-show="scope.row.children.length == 0" size="mini" 
            type="primary" plain @click="addCase(scope.row)">
            添加</el-button>
          <el-button v-show="scope.row.children.length == 0" size="mini" 
             type="primary" plain @click="approDetail(scope.row.id)">
             查看</el-button>
          <el-button v-show="scope.row.children.length == 0" 
           size="mini"type="primary" plain @click="approDel(scope.row.id)">
           删除</el-button>
      </template>
    </el-table-column>
</el-table>

这样写造成报错的原因是没有预先判断数组是否存在,需要先对数组进行非空验证:scope.row.children!= undefined,如下正确写法:

<el-table>
  <el-table-column label="操作" align="center" width="330">
      <template slot-scope="scope">
         <el-button v-show="scope.row.children!= undefined && 
            scope.row.children.length>0" size="mini" type="primary" plain 
            @click="dowload(scope.row)"> 
           预览</el-button>
          <el-button v-show="scope.row.children!= undefined && 
              scope.row.children.length>0" size="mini" type="primary" plain 
             @click="dowload(scope.row)"> 
           下载</el-button>
          <el-button v-show="scope.row.children!= undefined  && 
              scope.row.children.length == 0" size="mini" type="primary" plain 
              @click="addCase(scope.row)">
              添加</el-button>
          <el-button v-show="scope.row.children!= undefined && 
             scope.row.children.length == 0" size="mini" type="primary" plain 
              @click="approDetail(scope.row.id)">
              查看</el-button>
          <el-button v-show="scope.row.children!= undefined && 
              scope.row.children.length == 0" size="mini" type="primary" plain 
              @click="approDel(scope.row.id)">
              删除</el-button>
       </template>
   </el-table-column>
</el-table>

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值