gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05 一 過濾器寫法 {{ message | Filter}} 二 Vue自帶的過濾器:capitalize 功能:首字母大寫 上面代碼輸出:Abc 三 Vue自帶的過 ...
gitHub地址:https://github.com/lily1010/vue_learn/tree/master/lesson05
一 過濾器寫法
{{ 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>