在config文件夹中创建common.js文件:

const commonFn = function(){
  console.log("公共方法封装成功!")
}

//将方法暴露出来
export default {
  commonFn
}

在main.js引入全局方法:

//将公共方法挂载this
import config from './config/common'
Vue.prototype.$config = config

全局使用方法:

this.$config.commonFn()
//在文件处直接使用

配置完成后在项目的所有vue文件中就都可以使用该全局方法了。

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐