创建一个放置过滤器的集合的js文件
类似filters.js
// import parseTime, formatTime and set to filter
// export { parseTime, formatTime } from '@/utils'
/**
* Show plural label if time is plural number
* @param {number} time
* @param {string} label
* @return {string}
*/
function pluralize(time, label) {
if (time === 1) {
return time + label
}
return time + label + 's'
}
/**
* @param {number} time
*/
export function timeAgo(time) {
const between = Date.now() / 1000 - Number(time)
if (between < 3600) {
return pluralize(~~(between / 60), ' minute')
} else if (between < 86400) {
return pluralize(~~(between / 3600), ' hour')
} else {
return pluralize(~~(between / 86400), ' day')
}
}
在main.js中引入
import * as filters from './filters' // global filters
轮流注册
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key])
})