全局过滤文件src/filters
import Vue from 'vue'
Vue.filter('filterCoin', (val) => {
const coins = {
userye: '小叶',
useryi: '小艺',
usershen: '小深'
}
return coins[val]
})
// 字符串截取;名字超出...表示
Vue.filter('ellipsis', function (str, sub_length = 10) {
if (str == "" || str == null) return "";
var temp1 = str.replace(/[^\x00-\xff]/g, "**");
var temp2 = temp1.substring(0, sub_length);
var x_length = temp2.split("\*").length - 1;
var hanzi_num = x_length / 2;
sub_length = sub_length - hanzi_num; //实际需要sub的长度是总长度-汉字长度
var res = str.substring(0, sub_length);
if (sub_length < str.length) {
var end = res + "…";
} else {
var end = res;
}
return end;
})
Vue.filter('filterDecimal', (number, digit = 8) => {
return Number(number).toFixed(digit)
})
在文件src/main.js引入
import './filters'
在文件index.vue引入使用
<div>{{‘100.987654’ | filterDecimal(3)}}</div>//100.988
网友评论