參考: https://www.cnblogs.com/liujn0829/p/8622960.html
https://blog.csdn.net/z8735058/article/details/76824548
一、單個過濾器
參考 https://cn.vuejs.org/v2/guide/filters.html
二、多個過濾器
新建dfilter.js文件
const dfilters = { addZeroTwo: function(value) { var value = Math.round(parseFloat(value) * 100) / 100; //注: 一定要用var聲明,let會報錯 var arr = value.toString().split('.'); if (arr.length === 1) { return value.toString() + '.00'; } else { if (arr[1].length === 1) { return value.toString() + '0'; } } }, addZeroOne: function(value) { var value = Math.round(parseFloat(value) * 100) / 100; var arr = value.toString().split('.'); if (arr.length === 1) { return value.toString() + '.0'; } else { if (arr[1].length === 1) { return value.toString() + '0'; } } } } export default dfilters;
在main.js中引入並注冊(在new Vue前注冊)
import dfilters from '../static/js/dfilters'; for (let key in dfilters) { Vue.filter(key, dfilters[key]); }
在組件中使用
<span>原價:¥{{shopgoods.gprice|addZeroTwo}}</span>