前言:前端很容易碰到后端返回时间戳渲染时间格式的问题
一、使用步骤
1.定义data
代码如下(示例):
data () {
return {
time:'',
}
},
2.写filters函数(!注意这里filters跟methods平级)
代码如下(示例):
filters:{
//过滤器名
'hehe':(data,tag)=>{
var tag=tag||'.'
let y=(new Date(data)).getFullYear()
let m=(new Date(data)).getMonth()+1
let d=(new Date(data)).getDate()
console.log(y,m,d)
return `${y}年${tag}${m}月${tag}${d}日`
}
},
methods: {
}
3.给time赋值(一般都是接口返回的时间戳)
getDetails () {
this.$api.get(`www.baidu.com/hahah}`).then(res => {
this.time = res.data.createDate * 1000
})
},
4.在你想要展现的标签里渲染()
<el-form-item label="发布时间">
<el-tag type="info" class="width-percent-9">{{time|hehe('-')}}</el-tag>//这里的-可以自定义
</el-form-item>
5 转化后效果:
如果帮到了您,留个赞再走吧!!