From ee724fc5e6c281c587aaf85a2d358626ee50ffd2 Mon Sep 17 00:00:00 2001 From: linzhe Date: Tue, 13 Aug 2024 21:41:35 +0800 Subject: [PATCH 1/3] fix(runtime-core): fix inject with currentApp --- packages/runtime-core/src/apiInject.ts | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index f05d7333da6..26070fa2a2b 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -59,15 +59,19 @@ export function inject( // to support `app.use` plugins, // fallback to appContext's `provides` if the instance is at root // #11488, in a nested createApp, prioritize using the provides from currentApp - const provides = currentApp - ? currentApp._context.provides - : instance - ? instance.parent == null - ? instance.vnode.appContext && instance.vnode.appContext.provides - : instance.parent.provides - : undefined + const provides = instance + ? instance.parent == null + ? instance.vnode.appContext && instance.vnode.appContext.provides + : instance.parent.provides + : undefined - if (provides && (key as string | symbol) in provides) { + if (currentApp) { + const currentProvides = currentApp._context.provides + if ((key as string | symbol) in currentProvides) { + const target = currentProvides[key as string] + return target + } + } else if (provides && (key as string | symbol) in provides) { // TS doesn't allow symbol as index type return provides[key as string] } else if (arguments.length > 1) { From 9e6e15182470149fae4b0967020ce04ccad8814b Mon Sep 17 00:00:00 2001 From: linzhe Date: Tue, 13 Aug 2024 22:52:29 +0800 Subject: [PATCH 2/3] chore: update --- packages/runtime-core/__tests__/apiCreateApp.spec.ts | 2 ++ packages/runtime-core/src/apiInject.ts | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/__tests__/apiCreateApp.spec.ts b/packages/runtime-core/__tests__/apiCreateApp.spec.ts index b578fa55c9c..00eea6907f2 100644 --- a/packages/runtime-core/__tests__/apiCreateApp.spec.ts +++ b/packages/runtime-core/__tests__/apiCreateApp.spec.ts @@ -126,11 +126,13 @@ describe('api: createApp', () => { childApp.provide('foo', 2) expect(childApp.runWithContext(() => inject('foo'))).toBe(2) + expect(childApp.runWithContext(() => inject('bar'))).toBe('bar') return () => h('div') }, }) app.provide('foo', 1) + app.provide('bar', 'bar') expect(app.runWithContext(() => inject('foo'))).toBe(1) const root = nodeOps.createElement('div') diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index 26070fa2a2b..e7d5fdd5bb5 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -68,10 +68,10 @@ export function inject( if (currentApp) { const currentProvides = currentApp._context.provides if ((key as string | symbol) in currentProvides) { - const target = currentProvides[key as string] - return target + return currentProvides[key as string] } - } else if (provides && (key as string | symbol) in provides) { + } + if (provides && (key as string | symbol) in provides) { // TS doesn't allow symbol as index type return provides[key as string] } else if (arguments.length > 1) { From 75491d2047dc9c38b9b2d9ec70a3007604e7ea1c Mon Sep 17 00:00:00 2001 From: linzhe141 <1572213544@qq.com> Date: Wed, 14 Aug 2024 12:34:54 +0800 Subject: [PATCH 3/3] chore: update --- packages/runtime-core/src/apiInject.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/runtime-core/src/apiInject.ts b/packages/runtime-core/src/apiInject.ts index e7d5fdd5bb5..49610b4ab5a 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -59,11 +59,6 @@ export function inject( // to support `app.use` plugins, // fallback to appContext's `provides` if the instance is at root // #11488, in a nested createApp, prioritize using the provides from currentApp - const provides = instance - ? instance.parent == null - ? instance.vnode.appContext && instance.vnode.appContext.provides - : instance.parent.provides - : undefined if (currentApp) { const currentProvides = currentApp._context.provides @@ -71,6 +66,13 @@ export function inject( return currentProvides[key as string] } } + + const provides = instance + ? instance.parent == null + ? instance.vnode.appContext && instance.vnode.appContext.provides + : instance.parent.provides + : undefined + if (provides && (key as string | symbol) in provides) { // TS doesn't allow symbol as index type return provides[key as string]