vue3中勾销了过滤器filter,如果组件中独自应用能够用computed、watch来替换。然而想全局创立一个之前的vue2中的filter,咱们就要应用在vue全局中挂在属性或者办法。
1、关上main.js文件,写入本人的全局属性或者办法。
import { createApp } from 'vue'import ElementPlus from 'element-plus'import 'element-plus/lib/theme-chalk/index.css'import './assets/scss/global.scss'import App from '@/App.vue'import router from '@/router'import store from '@/store'// 开始const app = createApp(App)// 创立办法const dateTimeSub = (value) => { if (value) { return value.split(' ')[0] }}// 挂在全局办法app.config.globalProperties.$filters = dateTimeSub// 主见这里改为app.useapp.use(store).use(router).use(ElementPlus).mount('#app')
2、应用
// 引入getCurrentInstanceimport { reactive, ref, getCurrentInstance } from 'vue'
setup (props, {emit}) { // 获取全局属性和办法 const { ctx, proxy } = getCurrentInstance() // ctx和proxy都能够拜访到定义的全局办法,然而ctx只能在本地应用,线上环境应用proxy ... return { proxy }
<template> <div>{{proxy.$filters('2020-06-22 10:55')}}<div></template>