思路:使用过日期滤器过滤时间戳,使用插槽获取当前行信息并插入处理后的时间
1、下载 moment 插件 : yarn add moment
2、在 main.js 中引入插件 :import moment from 'moment/moment'
3、在main.js中全局封装过滤器
// 日期过滤器
Vue.filter('moment',function(value,str="YYYY年MM月DD日 HH:mm:ss"){
return moment(value).format(str)
})
// 其它过滤器
Vue.filter('payStatus', function (value) {
if (value == 'processing') {
return '处理中'
}
if (value == 'handleSuccess') {
return '销账成功'
}
return '支付失败'
})
参数说明:
value : 为第一个参数,即要我们过滤的内容。
str :为第二个参数,是默认的参数;当我们调用封装的过滤器函数时没有传参时会默认使用这个参数。
4、调用过滤器(即使用)
<!-- 使用日期过滤器 -->
<el-table-column prop="orderTime" label="交易时间">
<template slot-scope="scope">
{{scope.row.orderTime|moment}}
</template>
</el-table-column>
<!-- 其它过滤器 -->
<el-table-column prop="status" label="交易状态">
<template slot-scope="scope">
{{scope.row.status|payStatus}}
</template>
</el-table-column>
附加:
1、传参 scope.row.data|moment('想要传的第二个参数')
2、moment转化时间戳出现 Invalid Date报错,
解决办法:moment(Number(value)).format(str)
这种情况一般是数据在传递的过程中类型发生了变化,用Number()转化一下数字类型就好了.