From 7d84f7d85f9694f34a86afa8053966936aaaab42 Mon Sep 17 00:00:00 2001 From: zhiyuanzmj <260480378@qq.com> Date: Thu, 29 May 2025 11:40:09 +0800 Subject: [PATCH] feat(runtime-core): add generic option for getCurrentInstance --- .../src/componentCurrentInstance.ts | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/componentCurrentInstance.ts b/packages/runtime-core/src/componentCurrentInstance.ts index c091b9c693d..22e55610b3f 100644 --- a/packages/runtime-core/src/componentCurrentInstance.ts +++ b/packages/runtime-core/src/componentCurrentInstance.ts @@ -16,10 +16,25 @@ export let currentInstance: GenericComponentInstance | null = null export const getCurrentGenericInstance: () => GenericComponentInstance | null = () => currentInstance || currentRenderingInstance -export const getCurrentInstance: () => ComponentInternalInstance | null = () => - currentInstance && !currentInstance.vapor - ? (currentInstance as ComponentInternalInstance) +/** + * Retrieves the current component instance. + * + * @param generic - A boolean flag indicating whether to return a generic component instance. + * If `true`, returns a `GenericComponentInstance` including vapor instance. + * If `false` or unset, returns a `ComponentInternalInstance` if available. + * @returns The current component instance, or `null` if no instance is active. + */ +export function getCurrentInstance( + generic: true, +): GenericComponentInstance | null +export function getCurrentInstance( + generic?: boolean, +): ComponentInternalInstance | null +export function getCurrentInstance(generic?: boolean) { + return currentInstance && (generic || !currentInstance.vapor) + ? currentInstance : currentRenderingInstance +} export let isInSSRComponentSetup = false