您好,登錄后才能下訂單哦!
小編給大家分享一下Vue自帶的過濾器有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
vue自帶的9種過濾器
一、過濾器寫法
{{ message | Filter}}
二、Vue自帶的過濾器:capitalize
功能:首字母大寫
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> {{message | capitalize}} </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { message: "abc" } }) </script> </body> </html>
代碼輸出:Abc
三、Vue自帶的過濾器:uppercase
功能:全部大寫
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> {{message | uppercase}} </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { message: "abc" } }) </script> </body> </html>
代碼輸出:ABC
四、Vue自帶的過濾器:uppercase
功能:全部小寫
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> {{message | lowercase}} </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { message: "ABC" } }) </script> </body> </html>
代碼輸出:abc
五、Vue自帶的過濾器:currency
功能:輸出金錢以及小數點
參數:
第一個參數 {String} [貨幣符號] - 默認值: '$'
第二個參數 {Number} [小數位] - 默認值: 2
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> {{message | currency}} <!--輸出$123.47--> {{message | currency '¥' "1"}} <!--輸出$123.5--> </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { message: "123.4673" } }) </script> </body> </html>
六、Vue自帶的過濾器:pluralize
功能: 如果只有一個參數,復數形式只是簡單地在末尾添加一個 “s”。如果有多個參數,參數被當作一個字符串數組,對應一個、兩個、三個…復數詞。如果值的個數多于參數的個數,多出的使用最后一個參數。
參數:{String} single, [double, triple, ...
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> {{message}} {{message | pluralize 'item'}} <!--輸出: 1 item--> <ul v-for="item in lili"> <li> {{item}} {{item | pluralize 'item'}} <!--輸出: 1 item 2 items 3 items--> </li> </ul> <ul v-for="item in lili"> <li> {{item}} {{item | pluralize 'st' 'rd'}} <!--輸出: 1 st 2 rd 3 rd--> </li> </ul> <ul v-for="item in man"> <li> {{item}} {{item | pluralize 'item'}} <!--輸出: 1 item 2 items 3 items--> </li> </ul> <ul v-for="item in man"> <li> {{item}} {{item | pluralize 'st' 'rd'}} <!--輸出: 1 st 2 rd 3 rd--> </li> </ul> </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { message: 1, lili: [1,2,3], man: { name1: 1, name2: 2, name3: 3 } } }) </script> </body> </html>
七、Vue自帶的過濾器:debounce
(1)限制: 需在@里面使用
(2)參數:{Number} [wait] - 默認值: 300
(3)功能:包裝處理器,讓它延遲執行 x ms, 默認延遲 300ms。包裝后的處理器在調用之后至少將延遲 x ms, 如果在延遲結束前再次調用,延遲時長重置為 x ms。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> <button id="btn" @click="disappear | debounce 10000">點擊我,我將10秒后消失</button> </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", methods: { disappear: function () { document.getElementById("btn").style.display= "none"; } } }) </script> </body> </html>
八、Vue自帶的過濾器:limitBy
(1)限制:需在v-for(即數組)里面使用
(2)兩個參數:
第一個參數:{Number} 取得數量
第二個參數:{Number} 偏移量
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> <ul v-for="item in lili | limitBy 10"> <!--輸出1 2 3 4 5 6 7 8 9 10--> <li>{{item}}</li> </ul> <ul v-for="item in lili | limitBy 10 3"> <!--輸出 4 5 6 7 8 9 10 11 12 13--> <li>{{item}}</li> </ul> </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { lili: [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15] } }) </script> </body> </html>
九、Vue自帶的過濾器:filterBy
(1)限制:需在v-for(即數組)里面使用
(2)三個參數:
第一個參數: {String | Function} 需要搜索的字符串
第二個參數: in (可選,指定搜尋位置)
第三個參數: {String} (可選,數組格式)
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> <ul v-for="item in lili | filterBy 'o' "> <!--輸出oi oa lo ouo oala--> <li>{{item}}</li> </ul> <ul v-for="item in man | filterBy 'l' in 'name' "> <!--輸出lily lucy--> <li>{{item.name}}</li> </ul> <ul v-for="item in man | filterBy 'l' in 'name' 'dada' "> <!--輸出lily+undefined lucy+undefined undefined+lsh--> <li>{{item.name+"+"+item.dada}}</li> </ul> </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { lili: ["oi", "oa", "ll", "lo" ,"ouo" ,"kk" ,"oala"], man: [ //此處注意man是數組,不是對象 {name: "lily"}, {name: "lucy"}, {name: "oo"}, {dada: "lsh"}, {dada: "ofg"} ] } }) </script> </body> </html>
十、Vue自帶的過濾器:orderBy
(1)限制:需在v-for(即數組)里面使用
(2)三個參數:
第一個參數: {String | Array<String> | Function} 需要搜索的字符串
第二個參數: {String} 可選參數 order 決定結果升序(order >= 0)或降序(order < 0),默認是升序
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Vue自帶的過濾器</title> <meta name="viewport" content="width=device-width,initial-scale=1, minimum-scale=1,maximum-scale=1,user-scalable=no" /> <script src="../js/vue.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div class="test"> <!--遍歷數組--> <ul v-for="item in lili | orderBy 'o' 1"> <!--輸出kk ll oi--> <li>{{item}}</li> </ul> <ul v-for="item in lili | orderBy 'o' -1"> <!--輸出oi ll kk--> <li>{{item}}</li> </ul> <!--遍歷含對象的數組--> <ul v-for="item in man | orderBy 'name' 1"> <!--輸出Bruce Chuck Jackie--> <li>{{item.name}}</li> </ul> <ul v-for="item in man | orderBy 'name' -1"> <!--輸出Jackie Chuck Bruce--> <li>{{item.name}}</li> </ul> <!--使用函數排序--> <ul v-for="item in man | orderBy ageByTen"> <!--輸出Bruce Chuck Jackie--> <li>{{item.name}}</li> </ul> </div> <script type="text/javascript"> var myVue = new Vue({ el: ".test", data: { lili: ["oi", "kk", "ll"], man: [ //此處注意man是數組,不是對象 { name: 'Jackie', age: 62 }, { name: 'Chuck', age: 76 }, { name: 'Bruce', age: 61 } ] }, methods: { ageByTen: function () { return 1; } } }) </script> </body> </html>
以上是“Vue自帶的過濾器有哪些”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。