app.config.globalProperties:一个用于注册能够被应用内所有组件实例访问到的全局属性的对象。
注册:
// main.ts中
app.config.globalProperties.$msg = 'hello'
app.config.globalProperties.$add = (num1: number, num2: number): number => {
return num1 + num2
}
app.mount('#app')
组件实例中使用:
<template>
<div>{{ proxy.$msg }}aaa</div>
</template>
<script setup lang="ts">
import { getCurrentInstance } from 'vue'
const { proxy } = getCurrentInstance() as any
console.log(proxy.$msg)
console.log(proxy.$add(1, 3))
</script>