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

This commit is contained in:
远方os 2024-05-27 17:07:38 +08:00 committed by GitHub
parent a498b4ef15
commit ae36b1a664
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 8 additions and 3 deletions

View File

@ -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)