Vue-全局过滤器

2,773 阅读1分钟

Vue-全局过滤器

  • 新建一个 filters.js 文件;
import moment from 'moment'

/* 时间过滤器 - 年月日-时分 */
export const formatTime = value => moment(value).format('YYYY-MM-DD HH:mm') 

  • 在 main.js 中全局注入;
import * as custom from '@/common/js/filters' 
Object.keys(custom).forEach(item => Vue.filter(item, custom[item])) 
  • 使用:
<el-table-column label="开学时间" prop="open_time" header-align="center" align="center">
   <template slot-scope="scope">
    {{ scope.row.open_time | formatTime }}
  </template>
</el-table-column>