main.ts
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.config.globalProperties.$hello = 'hello world'
app.config.globalProperties.$sayHello = () => console.log('你好呀')
app.mount('#app')
Demo.vue
<script lang="ts">
import { defineComponent, getCurrentInstance } from 'vue'
declare module '@vue/runtime-core' {
interface ComponentCustomProperties {
$hello: string
$sayHello: () => void
}
}
export default defineComponent({
name: 'Demo-Component',
setup() {
const currentInstance = getCurrentInstance()
console.log(currentInstance?.proxy?.$hello)
currentInstance?.proxy?.$sayHello()
},
})
</script>