# Vue3.0 全局变量 app.config.globalProperties 使用

**URL:** <https://www.isharkfly.com/t/vue3-0-app-config-globalproperties/15757>\
**Category:** 前端\
**Tags:** vue\
**Created:** [May 17, 2024, 4:32pm UTC](https://www.isharkfly.com/t/vue3-0-app-config-globalproperties/15757 "2024-05-17T16:32:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![honeymoose](https://www.isharkfly.com/user_avatar/www.isharkfly.com/honeymoose/32/20796_2.png) [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Post date:** [May 17, 2024, 4:32pm UTC](https://www.isharkfly.com/t/vue3-0-app-config-globalproperties/15757/1 "2024-05-17T16:32:23Z")

</div>

app.config.globalProperties是一个用于注册能够被应用内所有组件实例访问到的全局属性的对象。

是Vue2中Vue.prototype使用的一种替代，具体用法如下：

```js
// main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
const app = createApp(App)
app.use(store).use(router).mount('#app')

```

app.config.globalProperties.message = ‘hello’// 自定义添加

1、在组合式api使用：

```js
<script setup>
// 通过getCurrentInstance 获取
import { getCurrentInstance, onMounted } from 'vue'
const { message } = getCurrentInstance().appContext.config.globalProperties
onMounted(() => {
  console.log(message) // hello
})
 
</script>

```

2、在选项api中使用：

```js
<script>
export default {
  mounted () {
    console.log(this.message) // hello
  }
}
</script>

```
