vue项目中main.js可以做哪些全局配置

98 阅读1分钟
1、 vue全局挂载的
// 1、注册指令 [如按钮权限控制]
Object.keys(directives).forEach((key) => {
  Vue.directive(key, (directives as { [key: string ]: DirectiveOptions })[key])
})

// 2、注册拦截器(过滤器)
Object.keys(filters).forEach((key) => {
  Vue.filter(key, (filters as { [key: string ]: Function })[key])
})

// 3、dayjs挂载到全局
Vue.prototype.$dayjs = dayjs

// 4、bus总线,把一些插件赋值到window对象上
window.eventHub = new Vue()
    // 业务中使用
    // window.eventHub.$emit('refreshFerryPlanTable')
    // window.eventHub.$on('refreshOrderTable', () => this.refreshTable())


// 5、公共api
Vue.prototype.$commonApi = commonApi
    // 使用
    // this.$commonApi.getOrgWorkBenchLimit('J00001', ['2'], query),

// 6、引入插件
Vue.use(ElementUI)
2、vue配置:Vue.config.xxx
// 1、线上环境屏蔽devtools
Vue.config.devtools = process.env.NODE_ENV !== 'production'

// 2、vue报错捕获
Vue.config.errorHandler = vueErrorHandler(自定义的方法)

3、整体原则
  • (1)vue全局注册过滤器,使用Vue.filter
  • (2)vue全局注册指令,使用Vue.directive
  • (3)在vue原型上添加方法 Vue.prototype
  • (4)在window对象上添加属性或方法 window.xxx = xxx
  • (5)修改vue的全局配置 Vue.config.xxx
  • (6)vue使用插件 Vue.use