局部過濾器:
<html> <head> <title>vue</title> <meta charset="utf-8"> </head> <body> <div id="app"> {{msg | toFixed(1)}} <!-- msg是參數input的值,方法里的參數都是作為input后的參數 --> </div> </body> <script src="node_modules\vue\dist\vue.js"></script> <script> let vm = new Vue({ el: "#app", filters: { // toFixed(input,param1,param2,param3) toFixed(input, num) { //第二個參數為小數點個數 return '¥' + input.toFixed(num); } }, data:{ msg:15.123 } }); </script> </html>
全局過濾器(記得放在所有實例之前)
<html> <head> <title>vue</title> <meta charset="utf-8"> </head> <body> <div id="app"> {{msg | my(2)}} <!-- msg是參數input的值,方法里的參數都是作為input后的參數 --> </div> </body> <script src="node_modules\vue\dist\vue.js"></script> <script> // 全局過濾 // Vue.filter(name,function) Vue.filter('my',(input,num)=>{ return '¥' + input.toFixed(num); }) // 局部過濾 let vm = new Vue({ el: "#app", filters: { // toFixed(input,param1,param2,param3) toFixed(input, num) { //第二個參數為小數點個數 return '¥' + input.toFixed(num); } }, data:{ msg:15.123 } }); </script> </html>