Vue3---Vue3中如何进行全局挂载
2021-06-27 本文已影响0人
小李不小
main.js中通过 config.globalProperties 进行全局挂载
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
const app = createApp(App)
app.use(store)
app.use(router)
app.mount('#app')
// Vue3 全局挂载系统名称
app.config.globalProperties.$systemName = '用户管理系统'
组件实例中通过 getCurrentInstance 获取proxy,再获取全局挂载的实例
<template>
<div>
<p>{{ sysName }}</p>
</div>
</template>
<script>
import { defineComponent, getCurrentInstance } from 'vue'
export default defineComponent({
name: '',
setup(){
const { proxy } = getCurrentInstance()
const sysName = proxy.$systemName
return {
sysName
}
}
})
</script>