基于element-ui table的二次封装以及slot的使用

插槽是什么?

插槽就是子组件中的提供给父组件使用的一个占位符,用 表示,父组件可以在这个占位符中填充任何模板代码,如 HTML、组件等,填充的内容会替换子组件的标签。简单理解就是子组件中留下个“坑”,父组件可以使用指定内容来补“坑”

$attrs是什么

包含了父作用域中不作为 prop 被识别 (且获取) 的 attribute 绑定 (class 和 style 除外)。当一个组件没有声明任何 prop 时,这里会包含所有父作用域的绑定 (class 和 style 除外),并且可以通过 v-bind=“$attrs” 传入内部组件——在创建高级别的组件时非常有用。

$listeners是什么

包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器。它可以通过 v-on=“$listeners” 传入内部组件——在创建更高层次的组件时非常有用。

下面给出一个基于element-ui table的基本封装实例

<template>
	<el-table
      ref="wxtTable"
      v-bind="$attrs"
      v-on="$listeners"
      element-loading-spinner="el-icon-loading"
      element-loading-text="拼命加载中"
      border
    >
      <template v-for="(item, index) in tableColumnList">
		<el-table-column
          :key="`col_${index}`"
          :prop="item.prop"
          min-width="100px"
          :align="item.align || 'center'"
          :width="item.width"
          :label="item.label"
          :resizable="item.resizable"
          :showOverflowTooltip="item.showOverflowTooltip"
          :fixed="item.fixed"
        >
          <template slot-scope="scope">
            <!--自定义作用域插槽-->
            <template v-if="item.slotName">
              <slot :name="item.slotName" :scope="scope"></slot>
            </template> </template>
            <span v-else>{{ scope.row[item.prop] }}</span>
          </template>
        </el-table-column>
      </template>
    </el-table>
</template>
	<script>
export default {
name: 'wxtTable',
  props: {
  	tableColumnList: {
      type: Array,
      default: () => []
    }, // 表格column
  },
}
</script>

页面中使用

<template>
	<wxt-table ref="wxtTable"
       	:data="tableList"
        :columnConfigList="columnConfigList">
        <template v-slot:peakRateAvg="{scope}">
			
		</template>
    </wxt-table>
</template>

<script>
import wxtTable from '@/components/wxtTable'
export default {
	components: {
    	wxtTable
  	},
  	data(){
		return {
			tableList:[],
			columnConfigList:[
				 { prop: 'date', label: '时间'},
		        { prop: 'name', label: '电源类型'},
		        { prop: 'maxRateAvg', label: '平均最大出力率' },
		        {
		          prop: 'peakRateAvg',
		          label: '平均高峰负荷时刻出力率',
		          slotName:'peakRateAvg'
		        }
			]
		}
	}
}
</script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue基于Element UI Table的二次封装可以通过创建一个自定义的组件来实现。以下是一个简单的示例,演示了如何封装一个基于Element UI Table的组件: ```vue <template> <el-table :data="tableData" :row-key="rowKey" :height="height"> <!-- 渲染表头 --> <el-table-column v-for="column in columns" :key="column.prop" :prop="column.prop" :label="column.label"> <!-- 自定义插槽 --> <template slot-scope="scope"> <slot :column="column" :scope="scope"></slot> </template> </el-table-column> </el-table> </template> <script> export default { name: 'CustomTable', props: { tableData: { type: Array, required: true }, columns: { type: Array, required: true }, rowKey: { type: String, required: true }, height: { type: String, default: 'auto' } } } </script> ``` 在这个示例中,我们创建了一个名为`CustomTable`的组件。它接受`tableData`、`columns`、`rowKey`和`height`作为props,分别表示表格数据、表格列配置、行数据的唯一标识以及表格的高度。 在模板中,我们使用`el-table`和`el-table-column`来渲染Element UI的表格。我们使用了`v-for`指令来循环渲染表格列,并通过`slot-scope`来传递数据给插槽。插槽可以在父组件中定义,并在插槽中使用自定义的组件来渲染表格单元格内容。 通过这种方式,我们可以在父组件中使用这个封装的自定义表格组件,并通过插槽来定制表格的内容和样式。 希望这个简单的示例能帮助到你进行Vue基于Element UI Table的二次封装。如果有任何问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值