1、main.js同级目录创建filters文件夹,文件夹下创建index.js
2、index.js
//格式化日期,2018-05-21T10:54:56.955+08:00 ——> 2018-05-21 18:54:56
const formatUtcTime = function(v) {
let date = new Date(v);
return date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + " " + date.getHours() + ":" + date.getMinutes() + ":" + date.getSeconds();
}
export {
formatUtcTime
}
3、main.js
//加载全局过滤器
import * as filters from './filters/index'
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key])
})
4、使用
html中
methods中