在vue3中安装dayjs包

2,180 阅读1分钟

app.config.globalProperties.$ 中可以定义新的全局属性,一般为$后加名字,也可以封装在单独文件中,然后在main.ts中进行注册

app.config.globalProperties.$filter = {
  foo() {
    console.log('2')
  },
  format(value: string) {
    return formatUtcString(value)
  }
}
//调用方法为:
$filter.format(value)

修改时间格式安装 npm install dayjs

    //导入文件
import dayjs from 'dayjs'
import utc from 'dayjs/plugin/utc'
    //扩展utc格式
dayjs.extend(utc)
const DATE_TIME_FORMAT = 'YYYY-MM-DD HH:mm:ss'
    //若未传format,则format默认为=右侧
export function formatUtcString(
  utcString: string,
  format1: string = DATE_TIME_FORMAT
) {
  return dayjs.utc(utcString).format(format1)
}//将传入的utcString format为format1格式