/ 前端
分类:前端来源:站内 最近更新:2020-09-19 16:46:15浏览:4207留言:0
在使用antd-vue版的table表格二次嵌套自定义组件时,原以为用<slot />插槽的方式就可以引入父级的所有子元素,结果根本不显示。
按照 Ant Design Vue 开发文档把插槽slot包裹起来,核心代码如下:
<a-table
v-bind="$attr"
@change="onChangeTable"
>
<!-- ..... -->
<!-- 自定义表格表头样式渲染项 -->
<template v-for="slotItem in headSlotColumns" :slot="slotItem.title">
<slot :name="slotItem.title"></slot>
</template>
<!-- 自定义表格行渲染项 -->
<template
v-for="slotItem in rowSlotColumns"
:slot="slotItem.customRender"
slot-scope="text, record, index"
>
<slot
:name="slotItem.customRender"
:text="text"
:record="record"
:index="index"
></slot>
</template>
<!-- ..... -->
</a-table>
<!-- ..... -->
computed: {
//筛选出表头元素插槽数组
headSlotColumns() {
return this.$attrs.columns
.filter(item => {
return item.slots;
})
.map(item => item.slots);
},
//筛选出表行元素内容插槽数组
rowSlotColumns() {
return this.$attrs.columns
.filter(item => {
return item.scopedSlots;
})
.map(item => item.scopedSlots);
},
}
<!-- ..... -->上一篇:JS乘除运算出现多位小数
下一篇:了解前端分分钟,一入前端毁终生