vue2.5入口文件配置全局属性

  • Vue 2.0之前我们封装axios和message等通用属性的时候是这么写的:
  1. import Vue from "vue";
  2. import echarts from "echarts";
  3. Vue.prototype.$echarts = echarts
  • 然后在组件内使用 this.$echarts就可以调用了,但是Vue3更新了以后这样写会报错

vue3入口文件配置全局属性

  1. const app=createApp(App);
  2. app.use(store);
  3. app.use(router);
  4. app.mount('#app');
  5. // 配置全局属性
  6. app.config.globalProperties.$echarts = echarts;
  • vue3获取vue实例
  1. import { getCurrentInstance } from 'vue'
  2. let $this = getCurrentInstance().appContext.config.globalProperties;
  3. console($this.$echarts)