理解vue之element-ui中的 <template slot-scope="scope">
如果有用过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(模版) 在这里属于一个固定用法: