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 f05d7333da6..49610b4ab5a 100644 --- a/packages/runtime-core/src/apiInject.ts +++ b/packages/runtime-core/src/apiInject.ts @@ -59,13 +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 + + if (currentApp) { + const currentProvides = currentApp._context.provides + if ((key as string | symbol) in currentProvides) { + 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