Vue中的過濾器(filter)詳解
官方文檔:https://cn.vuejs.org/v2/guide/filters.html
在官方文檔中,是這樣說明的:可被用于一些常見的文本格式化,vue中過濾器的作用可被用于一些常見的文本格式化。(也就是修飾文本,但是文本內(nèi)容不會改變)
個人覺得稱它為加工車間會更加貼切一些,過濾器可以用來篩選出符合條件的,丟棄不符合條件的;加工車間既可以篩選,又可以對篩選出來的進(jìn)行加工。
過濾器使用位置
過濾器可以用在兩個地方:雙花括號插值和 v-bind 表達(dá)式 (后者從 2.1.0+ 開始支持)

示例:


<template>
<div class="test">
<div :id="rawId">{{rawId}}</div>
<!-- 以下v-bind可省略,即v-bind:id可簡寫為:id -->
<div v-bind:id="rawId|filter_formatId">{{rawId|filter_formatId}}</div>
</div>
</template>
<script>
export default {
data() {
return {
rawId: 1
};
},
filters: {
filter_formatId(value) {
return value * 10;
}
}
};
</script>
<style lang="scss" scoped>
.test {
color: black;
}
</style>全局過濾器、局部過濾器
全局過濾器:
在main.js中寫入:

Vue.filter('filter_addPricePrefix', function (value) {
return "¥" + value;
})
局部過濾器:
在vue示例中寫入:

![]()
<template>
<div class="test">
<p>{{price}}</p>
<p>{{price | filter_addPricePrefix}}</p>
</div>
</template>
<script>
export default {
data() {
return {
price: 100
};
},
filters: {
filter_addPricePrefix(value) {
return "¥" + value;
}
}
};
</script>
<style lang="scss" scoped>
.test {
color: black;
}
</style>過濾器中傳入多個參數(shù):
過濾器是 JavaScript 函數(shù),因此可以接收參數(shù):
{{ message | filterA('arg1', arg2) }}這里,
filterA被定義為接收三個參數(shù)的過濾器函數(shù)。其中message的值作為第一個參數(shù),普通字符串'arg1'作為第二個參數(shù),表達(dá)式arg2的值作為第三個參數(shù)。


<template>
<div class="test">
<!-- 要過濾的數(shù)據(jù),永遠(yuǎn)是第一個參數(shù);通過filter函數(shù),傳遞的參數(shù),依次排在后面。 -->
<p>{{ new Date() | filter_dateFormat }}</p>
<p>{{ new Date() | filter_dateFormat('YYYY-MM-DD') }}</p>
<p>{{ new Date() | filter_dateFormat('YYYY-MM-DD', count) }}</p>
</div>
</template>
<script src="https://cdn.bootcss.com/moment.js/2.24.0/moment.js"></script>
<script>
export default {
data() {
return {
count: 10
};
},
filters: {
filter_dateFormat(date, format, count) {
return (
moment(date).format(format || "YYYY-MM-DD HH:mm:ss") +
(count ? " -- " + count : "")
);
}
}
};
</script>
<style lang="scss" scoped>
.test {
color: black;
}
</style>多個過濾器串聯(lián):
{{ message | filterA | filterB }}在這個例子中,
filterA被定義為接收單個參數(shù)的過濾器函數(shù),表達(dá)式message的值將作為參數(shù)傳入到函數(shù)中。然后繼續(xù)調(diào)用同樣被定義為接收單個參數(shù)的過濾器函數(shù)filterB,將filterA的結(jié)果傳遞到filterB中。


<template>
<div class="test">
<p>{{price}}</p>
<p>{{price | filter_addPricePrefix}}</p>
<p>{{price | filter_addPricePrefix |filter_addPriceSuffix}}</p>
</div>
</template>
<script>
export default {
data() {
return {
price: 100
};
},
filters: {
filter_addPricePrefix(value) {
return "¥" + value;
},
filter_addPriceSuffix(value) {
return value + "元";
}
}
};
</script>
<style lang="scss" scoped>
.test {
color: black;
}
</style>注意:
過濾器中通過this是獲取不到vue實例的?。?!在其中發(fā)起http請求也會失敗,因此,為了獲取后臺數(shù)據(jù),我們可以在mounted中先獲取tableData,然后,把這個tableData作為過濾器的第二個參數(shù)進(jìn)行傳遞?。。。?/p>


到此這篇關(guān)于Vue中的過濾器(filter)的文章就介紹到這了,更多相關(guān)Vue中的過濾器內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+watermark-dom實現(xiàn)頁面水印效果(示例代碼)
watermark.js 是基于 DOM 對象實現(xiàn)的 BS 系統(tǒng)的水印,確保系統(tǒng)保密性,安全性,降低數(shù)據(jù)泄密風(fēng)險,簡單輕量,支持多屬性配置,本文將通過 vue 結(jié)合 watermark-dom 庫,教大家實現(xiàn)簡單而有效的頁面水印效果,感興趣的朋友跟隨小編一起看看吧2024-07-07
Element-UI中關(guān)于table表格的那些騷操作(小結(jié))
這篇文章主要介紹了Element-UI中關(guān)于table表格的那些騷操作(小結(jié)),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Vue v2.4中新增的$attrs及$listeners屬性使用教程
這篇文章主要給大家介紹了關(guān)于Vue v2.4中新增的$attrs及$listeners屬性的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01
記一次Vue.js混入mixin的使用(分權(quán)限管理頁面)
這篇文章主要介紹了記一次Vue.js混入mixin的使用(分權(quán)限管理頁面),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-04-04
Vue Element前端應(yīng)用開發(fā)之動態(tài)菜單和路由的關(guān)聯(lián)處理
這篇文章主要介紹了Vue Element前端應(yīng)用開發(fā)之動態(tài)菜單和路由的關(guān)聯(lián)處理,對vue感興趣的同學(xué),可以參考下2021-05-05

