過濾器:
vue1.0 提供過濾器:html
系統就自帶不少過濾 capitalize uppercase currency json....
{{msg | currency}}
{{msg | json}}
....
limitBy
filterBy
.....
一些簡單功能,本身經過js實現
vue
debounce 配合事件,延遲執行web
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> <input type="text" @keyup="show | debounce 2000"> </div> <script> var vm=new Vue({ data:{ }, methods:{ show:function(){ alert(1); } } }).$mount('#box'); </script> </body> </html>
數據配合使用過濾器:
limitBy 限制幾個
limitBy 參數(取幾個)
limitBy 取幾個 從哪開始json
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <div id="box"> <ul> <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> </body> </html>
filterBy 過濾數據
filterBy ‘誰’api
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <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> </body> </html>
orderBy 排序
orderBy 誰 1/-1app
1 -> 正序
2 -> 倒序ide
自定義過濾器: model ->過濾 -> view工具
Vue.filter(name,function(input){ });
時間轉化器spa
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <style> </style> <script src="vue.js"></script> <script> </script> </head> <body> <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() }, methods:{ } }).$mount('#box'); </script> </body> </html>
model -> view(數據 -> 視圖)scala
view -> model
Vue.filter('filterHtml',{ read:function(input){ //model-view return input.replace(/<[^<+]>/g,''); }, write:function(val){ //view -> model return val; } });
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>智能社——http://www.zhinengshe.com</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"> <meta name="apple-mobile-web-app-capable" content="yes"> <meta name="apple-mobile-web-app-status-bar-style" content="black"> <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}}} </div> </body> </html>
vue2。0
到了2.0, 內置過濾器,所有刪除了
lodash 工具庫 _.debounce(fn,200)
自定義過濾器——還有
可是,自定義過濾器傳參
以前: {{msg | toDou '12' '5'}} 如今: {{msg | toDou('12','5')}}