
方案一:使用 formatter 属性(推荐)
<el-table-column
prop="time"
label="保质期"
align="center"
width="160"
:show-overflow-tooltip="true"
:formatter="dateFormat">
</el-table-column>
并在 methods 中添加格式化方法:
methods: {
// 添加日期格式化方法
dateFormat(row, column, cellValue) {
if (!cellValue) return '';
const date = new Date(cellValue);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
},
// 其他方法...
}
方案二:使用 template 和过滤器
<el-table-column
label="保质期"
align="center"
width="160"
:show-overflow-tooltip="true">
<template slot-scope="scope">
{{ scope.row.time| dateFormat }}
</template>
</el-table-column>
并在 filters 中添加过滤器:
filters: {
dateFormat(value) {
if (!value) return '';
const date = new Date(value);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, '0');
const day = String(date.getDate()).padStart(2, '0');
return `${year}-${month}-${day}`;
}
}
这里推荐使用方案一,因为它更符合 Element UI 的设计模式,代码也更简洁。
300

被折叠的 条评论
为什么被折叠?



