vue如何挂载全局共用方法

比如把echarts放到Vue原型上方便使用
vue2的写法

import Vue from 'vue'
Vue.prototype.$echarts = echarts

vue3的写法

import {
    
     createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.config.globalProperties.$echarts = echarts

注意在vue3的setup中已经无法像vue2中直接使用this.xxxx来使用公共方法。
vue3中的使用

 import {
    
     getCurrentInstance } from 'vue';
 const instance = getCurrentInstance();
 //使用全局方法
 instance.appContext.config.globalProperties

猜你喜欢

转载自blog.csdn.net/TwilighTzvz/article/details/127900412
今日推荐