在vue中绑定全局原型属性

在vue2中通过导入Vue构造函数再在上面绑定

// main.js
import Vue from "vue"
Vue.prototype.$test = "test"

// 组件中
mounted(){
	console.log(this.$test)
}

Vue3中需要使用一些api

// main.js
const app = createApp(App)
app.config.globalProperties.$test = "test"

// 组件中
<script setup>
import { getCurrentInstance } from "vue"
const internalInstance = getCurrentInstance();
const $test = internalInstance.appContext.config.globalProperties.$test;
</script>
posted @ 2021-09-14 15:49  影依贤者  阅读(339)  评论(0编辑  收藏  举报