Quellcode durchsuchen

refactor(runtime-core): prevent users from manually calling lifecycle hook function (#8731)

远方os vor 1 Jahr
Ursprung
Commit
ae36b1a664
1 geänderte Dateien mit 8 neuen und 3 gelöschten Zeilen
  1. 8 3
      packages/runtime-core/src/apiLifecycle.ts

+ 8 - 3
packages/runtime-core/src/apiLifecycle.ts

@@ -68,10 +68,15 @@ export function injectHook(
 
 export const createHook =
   <T extends Function = () => any>(lifecycle: LifecycleHooks) =>
-  (hook: T, target: ComponentInternalInstance | null = currentInstance) =>
+  (hook: T, target: ComponentInternalInstance | null = currentInstance) => {
     // post-create lifecycle registrations are noops during SSR (except for serverPrefetch)
-    (!isInSSRComponentSetup || lifecycle === LifecycleHooks.SERVER_PREFETCH) &&
-    injectHook(lifecycle, (...args: unknown[]) => hook(...args), target)
+    if (
+      !isInSSRComponentSetup ||
+      lifecycle === LifecycleHooks.SERVER_PREFETCH
+    ) {
+      injectHook(lifecycle, (...args: unknown[]) => hook(...args), target)
+    }
+  }
 
 export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)
 export const onMounted = createHook(LifecycleHooks.MOUNTED)