소스 검색

refactor: allow `getScopeOwner` to return null and add non-null assertion for instance access.

daiwei 4 달 전
부모
커밋
078e754eba
2개의 변경된 파일4개의 추가작업 그리고 4개의 파일을 삭제
  1. 1 1
      packages/runtime-vapor/src/component.ts
  2. 3 3
      packages/runtime-vapor/src/componentSlots.ts

+ 1 - 1
packages/runtime-vapor/src/component.ts

@@ -1032,5 +1032,5 @@ function handleSetupResult(
 
 export function getCurrentScopeId(): string | undefined {
   const scopeOwner = getScopeOwner()
-  return scopeOwner && scopeOwner.type.__scopeId
+  return scopeOwner ? scopeOwner.type.__scopeId : undefined
 }

+ 3 - 3
packages/runtime-vapor/src/componentSlots.ts

@@ -145,8 +145,8 @@ export function setCurrentSlotOwner(
  * Get the effective slot instance for accessing rawSlots and scopeId.
  * Prefers currentSlotOwner (if inside a slot), falls back to currentInstance.
  */
-export function getScopeOwner(): VaporComponentInstance {
-  return (currentSlotOwner || currentInstance) as VaporComponentInstance
+export function getScopeOwner(): VaporComponentInstance | null {
+  return (currentSlotOwner || currentInstance) as VaporComponentInstance | null
 }
 
 /**
@@ -180,7 +180,7 @@ export function createSlot(
   const _isLastInsertion = isLastInsertion
   if (!isHydrating) resetInsertionState()
 
-  const instance = getScopeOwner()
+  const instance = getScopeOwner()!
   const rawSlots = instance.rawSlots
   const slotProps = rawProps
     ? new Proxy(rawProps, rawPropsProxyHandlers)