创建filters.js 文件
/**
* 10000 => "10,000"
* @param {number} num
*/
export function toThousandFilter(num) {
return (+num || 0).toString().replace(/^-?\d+/g, m => m.replace(/(?=(?!\b)(\d{3})+$)/g, ','))
}
/**
* Upper case first char
* @param {String} string
*/
export function uppercaseFirst(string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
在mian.js 注册过滤器
import * as filters from './filters' // global filters
// register global utility filters
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key])
})
可在代码中直接使用
<span>{{value|uppercaseFirst}}<span>