前言
filter譯為過濾器袜香,一般用于數(shù)據(jù)的篩選,轉(zhuǎn)化,比如時(shí)間顯示做裙,后端傳給你一個(gè)毫秒值岗憋,你需要將至轉(zhuǎn)為yyyy-mm-dd,又或者后端傳給你一個(gè)value锚贱,你需要顯示對應(yīng)的key值仔戈。
正文
代碼實(shí)現(xiàn)
<template>
<div>
<div>日期:{{time | timeFormat}}</div>
<div>類型:{{type | typeFormat | chToEn}}</div>
</div>
</template>
<script>
export default {
data() {
return {
time: '1586571957384',
type: '2'
}
},
filters: {
timeFormat(val) {
let t = new Date(parseInt(val, 10))
return `${t.getFullYear()}-${t.getMonth()+1}-${t.getDate()}`
},
typeFormat(val) {
switch(val) {
case '1':
return '快樂';
case '2':
return '悲傷';
}
},
chToEn(val) {
switch(val) {
case '快樂':
return 'happy';
case '悲傷':
return 'sad';
}
}
}
}
</script>