做用:過濾數據
語法:
全局:
Vue.filter("過濾器名",(參數一,參數二)=>{});
參數一:須要過濾的數據
參數二:傳遞的數據
局部:
filters:{
過濾器名(){
}
}
使用:{{username|過濾器名()}}
步驟:
(1)時間過濾器
①聲明一個全局過濾器
Vue.filter("date",(data,icon)=>{
let year=(new Date(data)).getFullYear();
let month=(new Date(data)).getMonth()+1;
let day=(new Date(data)).getDate();
var icon=icon||"/";
return `${year}${icon}${month}${icon}${day}`;
});
②實例中設置一個time:
let vm=new Vue({
el:"#app",
data:{
time:(new Date()).getTime()
}
});
③管道符進行使用:
{{time|date("-")}}
(2)圖片尺寸過濾器
①data中將imgUrl引入:
data(){
return{
imgUrl:"http://p0.meituan.net/w.h/movie/2c24eb6a84a92b9ba837967851bec9462844109.jpg"
}
}
②聲明局部filters:
filters:{
imgReplace(data,wh){
// 將字符串 "w.h" 替換爲 "170.280"
return data.replace(/w\.h/,wh);
}
}
③頁面中使用:
<img :src="imgUrl|imgReplace('170.280')">