From c18eb2010729d4bdb6bf10728e8d688116fa02fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Thu, 6 Jul 2023 12:31:28 +0800 Subject: [PATCH 1/2] refactor(runtime-core): prevent users from manually calling lifecycle hook function --- packages/runtime-core/src/apiLifecycle.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/apiLifecycle.ts b/packages/runtime-core/src/apiLifecycle.ts index 0cd88846354..6fa474c4acc 100644 --- a/packages/runtime-core/src/apiLifecycle.ts +++ b/packages/runtime-core/src/apiLifecycle.ts @@ -65,10 +65,11 @@ export function injectHook( export const createHook = 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) + ;(!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) From c10a02e5a61be2a595c9c4c74c537e6890658668 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=B6=E8=BF=9C=E6=96=B9?= Date: Wed, 25 Oct 2023 09:23:15 +0800 Subject: [PATCH 2/2] chore: use `if` instead of `&&` --- packages/runtime-core/src/apiLifecycle.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/runtime-core/src/apiLifecycle.ts b/packages/runtime-core/src/apiLifecycle.ts index 6fa474c4acc..4f76480e454 100644 --- a/packages/runtime-core/src/apiLifecycle.ts +++ b/packages/runtime-core/src/apiLifecycle.ts @@ -67,8 +67,12 @@ export const createHook = any>(lifecycle: LifecycleHooks) => (hook: T, target: ComponentInternalInstance | null = currentInstance) => { // post-create lifecycle registrations are noops during SSR (except for serverPrefetch) - ;(!isInSSRComponentSetup || lifecycle === LifecycleHooks.SERVER_PREFETCH) && + if ( + !isInSSRComponentSetup || + lifecycle === LifecycleHooks.SERVER_PREFETCH + ) { injectHook(lifecycle, (...args: unknown[]) => hook(...args), target) + } } export const onBeforeMount = createHook(LifecycleHooks.BEFORE_MOUNT)