Vue3 全局挂载

main.js中通过 config.globalProperties 进行全局挂载

import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App);
// Vue3全局挂载名称
app.config.globalProperties.$vueName = 'Vue3全局挂载名称'
app.mount('#app')

获取方法通过 getCurrentInstance 获取proxy,再获取全局挂载的实例

import { defineComponent, getCurrentInstance } from 'vue'
export default defineComponent({
  setup(){
    const { proxy } = getCurrentInstance()
    console.log(proxy.$vueName)
    return {}
  }
})

你可能感兴趣的:(Vue3 全局挂载)