1、组件内定义及使用
(1)组件内定义
// 定义过滤器
filters:{
formatDate(date){
const nDate=new Date(date)
const year=nDate.getFullYear()
const month=(nDate.getMonth()+1).toString().padStart(2,0)
const day=nDate.getDate().toString().padStart(2,0)
return year+"-"+month+"-"+day
},
lengthLimit(value){
if(value.length<14){
return value;
}else{
return value.slice(0,10)+"..."
}
}
},
(2)使用
<text class="time">上映日期: {{item.time |formatDate}}</text>
2、全局定义及使用
(1)【main.js】中定义
// 定义全局的过滤器
Vue.filter("formatDate",(date)=>{
const nDate=new Date(date)
const year=nDate.getFullYear()
const month=(nDate.getMonth()+1).toString().padStart(2,0)
const day=nDate.getDate().toString().padStart(2,0)
return year+"-"+month+"-"+day
})
在任何页面均可使用,使用方法同1(2)