VUE过滤器
程序员文章站
2022-06-04 14:36:07
...
VUE过滤器
<div id='app'>
<p>{{bir|birTo_age}}</p>
<p>{{bir|birTo_age2}}</p>
<p>{{create_at|sinatime_userUI}}</p>
</div>
<script>
//全局
Vue.filter("birTo_age", function(olddata) {
return parseFloat(new Date().getFullYear()) - olddata
})
var vm = new Vue({
el: '#app',
data: {
bir: 1998,
create_at: "Tue May 21 13:52:13 +0800 2019"
},
filters: {
birTo_age2: function(olddata) {
return parseFloat(new Date().getFullYear()) - olddata
},
sinatime_userUI: function(olddata) {
var sinadt = new Date(olddata)
var nowdt = new Date()
var absdt = nowdt - sinadt
console.log(absdt)
if (0 > absdt) {
return "未来"
} else if (0 <= absdt && absdt < 60 * 1000) {
return "刚刚"
} else if (60 * 1000 <= absdt && absdt < 60 * 60 * 1000) {
return `${new Date(absdt).getMinutes()}分钟前`
} else if (60 * 60 * 1000 <= absdt && absdt < 24 * 60 * 60 * 1000) {
return `${sinadt.getHours()}:${sinadt.getMinutes()}`
} else if (24 * 60 * 60 * 1000 <= absdt && absdt < 30 * 24 * 60 * 60 * 1000) {
return `${sinadt.getMonth()+1}月${sinadt.getDate()}日 ${sinadt.getHours()}:${sinadt.getMinutes()}`
}
}
}
})
</script>