vue-cli中定义全局filter方法

1、创建 filters.js 文件:

2、filters.js 中定义全局过滤方法:

// 时间戳转换时间
exports.showTime = (time) => {
  let date = null
  if ((time + '').length === 10) {
    date = new Date(time * 1000)
  } else {
    date = new Date(time)
  }
  const Y = date.getFullYear()
  const M = date.getMonth() + 1
  const D = date.getDate()
  const H = date.getHours()
  const MM = date.getMinutes()
  const S = date.getSeconds()
  return `${Y}-${(M > 9 ? M : ('0' + M))}-${(D > 9 ? D : ('0' + D))} ${(H > 9 ? H : ('0' + H))}:${(MM > 9 ? MM : ('0' + MM))}:${(S > 9 ? S : ('0' + S))}`
}

// 状态类型判断
exports.showType = (type) => {
  const Obj = {
  '1': '成功',
  '2': '失败'
  '3': '无结果'
 }
 return Obj[type]
}

3、main.js入口文件引用

import filters from './filters'
Object.keys(filters).forEach(k => Vue.filter(k, filters[k]))

4、组建中使用

<template>
    <div>{{ 1454664434 | showTime }}</div>
</template>

视图显示

猜你喜欢

转载自www.cnblogs.com/zhaoxiaoying/p/10723063.html