提示
我选用的是Vue 过滤器使用,个人觉得比较方便,不过过滤器不支持Vue3,你可以封装成方法嘛都行,下面我以过滤器的形式展示出来
filters: {
// 截取字符串
subStringText(value, index) {
const str = String(value);
if (!value) return 0;
return str.length > index ? str.substring(0, index) + '...' : str;
},
// 格式化数字
formatNumber(num,decimals) {
num = num.toFixed(decimals);
num += '';
const x = num.split('.');
let x1 = x[0];
const x2 = x.length > 1 ? '.' + x[1] : '';
const rgx = /(\d+)(\d{3})/;
if (',' && isNaN(parseFloat(','))) {
while (rgx.test(x1)) {
x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
}
return x1 + x2;
},
},
使用:
有个插件 -- 可以轻松实现数字滚动并且数字格式化,推荐vue-count-to ,非常友好,