理解vue之element-ui中的 <template slot-scope="scope">

本文解析了Element UI中Table组件的高级用法,重点介绍了&lt;templateslot-scope=scope&gt;的使用技巧及scope参数的意义,帮助开发者更好地理解和应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如果有用过element-ui中的table组件,可能会发现有这么一个写法:

<template slot-scope="scope">

在实际的使用过程中,这种用法当然不仅仅局限于此,其他的地方也会用到。到底这里有什么特别之处呢?

我们看看普通的table用法:

我们先说一说这个基础的用法里面,在el-table中,:data="tableData"是数据集,结构如下:

那么对于每一个el-table-column,我们只需要使用prop="date",就可以将该列的数据绑定为该数组所有的对象中的“date”属性,我们可以理解为对于tableData,这里始终取的是tableData[$index].date。

table按照tableData这个数组的长度来生成多少行,按照有多少个el-table-column来生成多少列。

 

现在我们可以看更高级的用法,也就是我们标题提到的<template slot-scope="scope">

    <el-table-column
      label="日期"
      width="180">
      <template slot-scope="scope">
        <i class="el-icon-time"></i>
        <span style="margin-left: 10px">{{ scope.row.date }}</span>
      </template>
    </el-table-column>

按照我们前面的理解,按照有多少个el-table-column来生成列,因此这里没有使用prop="date",生成的单元格也就是空白的一个单元格。

template(模版) 在这里属于一个固定用法: <template slot-scope="scope">

我们主要说一下这个scope是个什么东西,按照element上的提示:

通过 Scoped slot 可以获取到 row, column, $index 和 store(table 内部的状态管理)的数据

我们可以理解为:tableData是给到table的记录集,scope是table内部基于tableData生成出来的,我们可以用Excel描绘一下

我们传进去的tableData,在table内部生成了类似于Excel的scope,因此,通过scope.row.date,我们就可以读取到每一行中的date。

还有重要的一点,scope又并非是整个table,我们只是能通过scope.row获得当前的行数据,至于具体为什么,目前我还没有理解得很透彻。只是希望按照这个理解,能记住多点关于scope的使用。

 

本文纯属个人的理解,如有不当的地方敬请指点!

<template> <div> <div> <!--搜索栏--> <el-input v-model="name" placeholder="请输入菜单名称" style="width: 200px" prefix-icon="el-icon-user"></el-input> <el-button class="el-icon-search" style="margin-left: 10px" type="primary" @click="load">查询</el-button> <el-button class="el-icon-refresh" style="margin-left: 10px" type="warning" @click="reset">重置</el-button> </div> <div style="margin-top: 20px;margin-bottom:35px;"> <!-- 新增、批量删除 --> <el-button class="el-icon-plus" style="margin-right: 10px" type="success" @click="save(null)">新增</el-button> <el-button class="el-icon-close" style="margin-left: 10px" type="danger" @click="deleteByids" >批量删除</el-button> </div> <el-table :data="tableData" border stripe :header-cell-style="getRowClass" @selection-change="handleSelectionChange" row-key="id" border default-expand-all> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="id" label="id"></el-table-column> <el-table-column prop="name" label="菜单名称"></el-table-column> <el-table-column prop="path" label="菜单路径"></el-table-column> <el-table-column prop="icon" label="菜单图标"> <template v-slot:default="scope"> <i :class="scope.row.icon"></i> </template> </el-table-column> <el-table-column prop="pagePath" label="页面路径"></el-table-column> <el-table-column prop="description" label="菜单描述"></el-table-column> <el-table-column prop="sortNum" label="排序"></el-table-column> <el-table-column label="操作" width="300"> <template v-slot="scope"> <div class="action-buttons"> <!-- 添加容器方便布局 --> <el-button type="success" size="mini" class="compact-btn" @click="save(scope.row.id)" v-if="!scope.row.pid && !scope.row.path">新增子菜单</el-button> <el-button type="prima
最新发布
03-29
评论 50
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

潇I洒

创作不易,觉得有用就鼓励一下吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值