element-ui动态设置el-table-column,并同时使用formatter和slot
模板渲染
<el-table-column
v-for="(row, index) in tableColumns"
:key="index"
:label="row.label"
:prop="row.key"
>
<template slot-scope="scope">
<span>{{ row.formatter ? row.formatter(scope.row) : scope.row[row.key] }}</span>
</template>
</el-table-column>
表头数据
const tableColumns = [
{
key: 'user',
label: '用户'
}, {
key: 'date',
label: '日期',
formatter: (row) => {
const pattern = /(\d{4})(\d{2})(\d{2})/
return row.date ? row.date.replace(pattern, '$1-$2-$3') : ''
}
}
]