vue過濾器用法實(shí)例分析
本文實(shí)例講述了vue過濾器用法。分享給大家供大家參考,具體如下:
過濾器:
vue提供過濾器:
capitalize uppercase currency....
<div id="box">
{{msg|currency ¥}}
</div>
debounce 配合事件,延遲執(zhí)行
<div id="box">
<input type="text" @keyup="show | debounce 2000">
</div>
數(shù)據(jù)配合使用過濾器:
limitBy 限制幾個(gè)
limitBy 參數(shù)(取幾個(gè))
limitBy 取幾個(gè) 從哪開始
<div id="box">
<ul>
<!--取2個(gè)-->
<li v-for="val in arr | limitBy 2">
{{val}}
</li>
<br/>
<br/>
<!--取2個(gè),從第arr.length-2個(gè)開始取-->
<li v-for="val in arr | limitBy 2 arr.length-2">
{{val}}
</li>
</ul>
</div>
<script>
var vm=new Vue({
data:{
arr:[1,2,3,4,5]
},
methods:{
}
}).$mount('#box');
</script>
filterBy 過濾數(shù)據(jù)
filterBy '誰'
<div id="box">
<input type="text" v-model="a">
<ul>
<li v-for="val in arr | filterBy a">
{{val}}
</li>
</ul>
</div>
<script>
var vm=new Vue({
data:{
arr:['width','height','background','orange'],
a:''
},
methods:{
}
}).$mount('#box');
</script>
orderBy 排序
orderBy 誰 1/-1
1 -> 正序
2 -> 倒序
<div id="box">
<input type="text" v-model="a">
<ul>
<li v-for="val in arr | orderBy -1">
{{val}}
</li>
</ul>
</div>
<script>
var vm=new Vue({
data:{
arr:['width','height','background','orange'],
a:''
},
methods:{
}
}).$mount('#box');
</script>
自定義過濾器: model ->過濾 -> view
Vue.filter(name,function(input){
});
<div id="box">
{{a | toDou 1 2}}
</div>
<script>
Vue.filter('toDou',function(input,a,b){
alert(a+','+b);
return input<10?'0'+input:''+input;
});
var vm=new Vue({
data:{
a:9
},
methods:{
}
}).$mount('#box');
</script>

時(shí)間轉(zhuǎn)化器
<div id="box">
{{a | date}}
</div>
<script>
Vue.filter('date',function(input){
var oDate=new Date(input);
return oDate.getFullYear()+'-'+(oDate.getMonth()+1)+'-'+oDate.getDate()+' '+oDate.getHours()+':'+oDate.getMinutes()+':'+oDate.getSeconds();
});
var vm=new Vue({
data:{
a:Date.now()//返回1970 年 1 月 1日午夜與當(dāng)前日期和時(shí)間之間的毫秒數(shù)。
},
methods:{
}
}).$mount('#box');
</script>
過濾html標(biāo)記
雙向過濾器:*
Vue.filter('filterHtml',{
read:function(input){ //model-view
return input.replace(/<[^<+]>/g,'');
},
write:function(val){ //view -> model
return val;
}
});
數(shù)據(jù) -> 視圖
model -> view
view -> model
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
<style>
</style>
<script src="vue.js"></script>
<script>
//<h2>welcome</h2>
Vue.filter('filterHtml',{
read:function(input){ //model-view
alert(1);
return input.replace(/<[^<]+>/g,'');
},
write:function(val){ //view -> model
console.log(val);
return val;
}
});
window.onload=function(){
var vm=new Vue({
data:{
msg:'<strong>welcome</strong>'
}
}).$mount('#box');
};
</script>
</head>
<body>
<div id="box">
<input type="text" v-model="msg | filterHtml">
<br>
{{msg | filterHtml}}
</div>
</body>
</html>
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
相關(guān)文章
vuex中store存儲(chǔ)store.commit和store.dispatch的區(qū)別及說明
這篇文章主要介紹了vuex中store存儲(chǔ)store.commit和store.dispatch的區(qū)別及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09
解決Vue的項(xiàng)目使用Element ui 走馬燈無法實(shí)現(xiàn)的問題
這篇文章主要介紹了解決Vue的項(xiàng)目使用Element ui 走馬燈無法實(shí)現(xiàn)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue的異步數(shù)據(jù)更新機(jī)制與$nextTick用法解讀
這篇文章主要介紹了vue的異步數(shù)據(jù)更新機(jī)制與$nextTick用法解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
快速搭建vue2.0+boostrap項(xiàng)目的方法
這篇文章主要介紹了快速搭建vue2.0+boostrap項(xiàng)目的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
vue-axios同時(shí)請(qǐng)求多個(gè)接口 等所有接口全部加載完成再處理操作
這篇文章主要介紹了vue-axios同時(shí)請(qǐng)求多個(gè)接口 等所有接口全部加載完成再處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-11-11
Vue項(xiàng)目中npm?install卡住問題解決的詳細(xì)指南
這篇文章主要介紹了Vue項(xiàng)目中npm?install卡住問題解決的相關(guān)資料,文中包括更換npm鏡像源、清除npm緩存、刪除.npmrc文件和升級(jí)Node.js版本,需要的朋友可以參考下2024-12-12
Vue頁面手動(dòng)刷新,實(shí)現(xiàn)導(dǎo)航欄激活項(xiàng)還原到初始狀態(tài)
這篇文章主要介紹了Vue頁面手動(dòng)刷新,實(shí)現(xiàn)導(dǎo)航欄激活項(xiàng)還原到初始狀態(tài),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-08-08

