【vue3】vue原型挂载全局变量和获取的方法
vue2.x挂载全局是使用Vue.prototype.xxxx=xxx的形式来挂载,然后通过this.xxxx=xxx的形式来挂载,然后通过this.xxxx=xxx的形式来挂载,然后通过this.xxx来获取挂载到全局的变量或者方法但是在vue3.x这种方法行不通了,在setup里面连this都获取不到,那么我们怎么挂载全局变量呢并使用它呢?1. 挂载在main.js中进行挂载import {
vue2.x挂载全局是使用Vue.prototype.
x
x
x
x
=
x
x
x
的
形
式
来
挂
载
,
然
后
通
过
t
h
i
s
.
xxxx=xxx的形式来挂载,然后通过this.
xxxx=xxx的形式来挂载,然后通过this.xxx来获取挂载到全局的变量或者方法
但是在vue3.x这种方法行不通了,在setup里面连this都获取不到,那么我们怎么挂载全局变量呢并使用它呢?
1. 挂载
在main.js中进行挂载
import { createApp } from 'vue'
import App from './App'
const app = createApp(App)
app.config.globalProperties.$httpUrl = 'https://www.baidu.com'
app.mount('#app')
2. 获取
import { getCurrentInstance } from 'vue'
const MyComponent = {
setup() {
const internalInstance = getCurrentInstance()
internalInstance.appContext.config.globalProperties.$httpUrl
// 访问 globalProperties
}
}
更多推荐
所有评论(0)