[Vue基础实战]过滤器的使用
参考代码:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>过滤器的使用</title> </head> <body> <div id="app"> <h1>{{ price | toFixed(2) | toUSD }}</h1> <h1>{{ strval | suffix(3,"***") }}</h1> </div> <script src="../js/vue.js"></script> <script type="text/javascript"> Vue.filter('toFixed', function (price, limit){ return price.toFixed(limit) }) Vue.filter('toUSD',function (tofixed){ return "$"+tofixed }) const app = new Vue({ el: '#app', data:{ price:435.333, strval:"htzd1234" }, filters: { suffix(text, length, suffixchar = '...') { return text.substring(0, length) + suffixchar; } }, }) </script> </body> </html>