JavaScript/vue.js/getCurrentInstance
来自康健生活
< JavaScript | vue.js
标题文本
获取全局注册组件
最简单的方法
window.app = app;
常规方法
<template>
<EchartsLinePanel
v-if="options"
:layerOptions="options"
@update:layerOptions="handleUpdateOptions"
/>
</template>
<script lang="ts" setup>
import { defineAsyncComponent, getCurrentInstance, type ComponentInternalInstance } from 'vue'
const { appContext } = <ComponentInternalInstance>getCurrentInstance()
const EchartsLinePanel = defineAsyncComponent({
loader: async () => {
// 获得 athena-echarts-line 组件
return appContext.components['athena-echarts-line']
},
})
</script>
另外一个思路
// define a plugin
const key = "__CURRENT_APP__"
export const ProvideAppPlugin = {
install(app, options) {
app.provide(key, app)
}
}
export function useCurrentApp() {
return inject(key)
}
// when create app use the plugin
createApp().use(ProvideAppPlugin)
// get app instance in Component.vue
const app = useCurrentApp()
return () => h(app.version)
No categories