這篇文章主要介紹了怎么在vue項(xiàng)目中實(shí)現(xiàn)一個(gè)過濾器功能,此處通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考價(jià)值,需要的朋友可以參考下:

Vue是一款友好的、多用途且高性能的JavaScript框架,使用vue可以創(chuàng)建可維護(hù)性和可測(cè)試性更強(qiáng)的代碼庫(kù),Vue允許可以將一個(gè)網(wǎng)頁(yè)分割成可復(fù)用的組件,每個(gè)組件都包含屬于自己的HTML、CSS、JavaScript,以用來渲染網(wǎng)頁(yè)中相應(yīng)的地方,所以越來越多的前端開發(fā)者使用vue。
過濾器
1.過濾器規(guī)則
Vue.js 允許你自定義過濾器,可被用于一些常見的文本格式化。過濾器可以用在兩個(gè)地方:
雙花括號(hào)插值{{}}和 v-bind 表達(dá)式 (后者從 2.1.0+ 開始支持)。過濾器應(yīng)該被添加在 JavaScript 表達(dá)式的尾部,由“管道”符號(hào)指示:
<!-- 在雙花括號(hào)中 -->
{{ name | Upper }}
<!-- 在 `v-bind` 中 -->
<div v-bind:id="martin | Upper"></div>過濾器分為全局過濾器和本地過濾器,全局過濾器顧名思義就是所有Vue實(shí)例掛載的元素內(nèi)都能使用,而本地過濾器則是指只有過濾器函數(shù)所在的Vue實(shí)例掛載的元素內(nèi)可以使用
全局過濾器:
Vue.filter('Upper',function (name) {
return name.toUpperCase();
});本地過濾器:
var vm=new Vue({
el: '#app',
data: {
name:'martin'
},
filters:{
Upper:function (name) {
return name.toUpperCase()
}
}
})2.串聯(lián)過濾器
{{name | filterA | filterB }}
解釋:
第一步:先把name 放到 filterA過濾器中進(jìn)行過濾
第二步:將第一步過濾器的結(jié)果再放到 filterB再進(jìn)行過濾,顯示最終過濾結(jié)果
3.過濾器也可以接收參數(shù),因?yàn)檫^濾器說到底只是一個(gè)函數(shù)
{{ name | filterA('arg1', arg2) }}
解釋:
filterA 在這里應(yīng)該定義為接收三個(gè)參數(shù)的過濾器函數(shù)。其中 name 的值作為第一個(gè)參數(shù),字符串 arg1 作為第二個(gè)參數(shù),表達(dá)式 arg2 的值作為第三個(gè)參數(shù)。
最后送給大家一個(gè)實(shí)例:

源代碼:
<!DOCTYPE html>
<html lang="en">
<head> <meta charset="UTF-8">
<title>Title</title>
<script src="./bootstrap/js/jquery-1.10.1.js"></script>
<link rel="stylesheet" href="./bootstrap/css/bootstrap.min.css">
<script src="./bootstrap/js/bootstrap.min.js"></script>
</head>
<body>
<div id="app">
<div class="form-inline">
<label>id:
<input type="text" class="form-control" placeholder="請(qǐng)輸入你的id" v-model="id">
</label>
<label>name:
<input type="text" class="form-control" placeholder="請(qǐng)輸入你的name" v-model="name">
</label>
<button class="btn btn-success" @click="add">add</button>
<label>請(qǐng)輸入你的搜索關(guān)鍵字:
<input type="text" class="form-control" placeholder="請(qǐng)輸入你的搜索關(guān)鍵字" v-model="keyword">
</label>
<button class="btn btn-success" @click="search(keyword)">search</button>
</div>
<table class="table table-bordered table-hover table-striped">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>ctime</th>
<th>operation</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,i) in search(keyword)" :key="item.id">
<td>{{item.id}}</td>
<td>{{item.name}}</td>
<td>{{item.ctime|dateFormat}}</td>
<td>
<a href="#" class="btn btn-success" @click.prevent="">{{item.operation[0]}}</a>
<a href="#" class="btn btn-success" @click.prevent="del(i)">{{item.operation[1]}}</a>
</td>
</tr>
</tbody>
</table>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script>
Vue.filter('dateFormat',function (date) {
var dy=date.getFullYear();
var dm=date.getMonth()+1;
var dd=date.getDate();
var dh=date.getHours();
var dm=date.getMinutes();
var ds=date.getSeconds();
return `${dy}-${dm}-${dd} ${dh}:${dm}:${ds}`
});
var vm=new Vue({
el: '#app',
data: {
keyword:'',
id:'',
name:'',
list:[{id:1,name:'寶馬',ctime:new Date(),operation:['add','delete']},
{id:2,name:'奔馳',ctime:new Date(),operation:['add','delete']},
{id:3,name:'大眾',ctime:new Date(),operation:['add','delete']}
]
},
methods:{
add(){
var curid=this.id;
var curname=this.name;
this.list.push({id:curid,name:curname,ctime:new Date(),operation:['add','delete']});
this.id='';
this.name='';
},
del(i){
this.list.splice(i,1);
},
search(name){
var arr=[];
this.list.forEach((item,i)=>{
if(item.name.indexOf(name)!=-1){
arr.push(item);
}
});
return arr
}
}
})
</script>
</body>
</html>到此這篇關(guān)于怎么在vue項(xiàng)目中實(shí)現(xiàn)一個(gè)過濾器功能的文章就介紹到這了,更多相關(guān)怎么在vue項(xiàng)目中實(shí)現(xiàn)一個(gè)過濾器功能的內(nèi)容請(qǐng)搜索創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持創(chuàng)新互聯(lián)成都網(wǎng)站設(shè)計(jì)公司!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享標(biāo)題:怎么在vue項(xiàng)目中實(shí)現(xiàn)一個(gè)過濾器功能-創(chuàng)新互聯(lián)
鏈接分享:http://chinadenli.net/article2/dspdic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、面包屑導(dǎo)航、品牌網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、搜索引擎優(yōu)化、小程序開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容