Skip to content

Commit 7e75de0

Browse files
authoredAug 7, 2024··
fix(runtime-core): prioritize using the provides from currentApp in nested createApp (#11502)
close #11488
1 parent 81351dc commit 7e75de0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed
 

‎packages/runtime-core/__tests__/apiCreateApp.spec.ts

+13
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,25 @@ describe('api: createApp', () => {
116116
const app = createApp({
117117
setup() {
118118
provide('foo', 'should not be seen')
119+
120+
// nested createApp
121+
const childApp = createApp({
122+
setup() {
123+
provide('foo', 'foo from child')
124+
},
125+
})
126+
127+
childApp.provide('foo', 2)
128+
expect(childApp.runWithContext(() => inject('foo'))).toBe(2)
129+
119130
return () => h('div')
120131
},
121132
})
122133
app.provide('foo', 1)
123134

124135
expect(app.runWithContext(() => inject('foo'))).toBe(1)
136+
const root = nodeOps.createElement('div')
137+
app.mount(root)
125138

126139
expect(
127140
app.runWithContext(() => {

‎packages/runtime-core/src/apiInject.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,14 @@ export function inject(
5656
// #2400
5757
// to support `app.use` plugins,
5858
// fallback to appContext's `provides` if the instance is at root
59-
const provides = instance
60-
? instance.parent == null
61-
? instance.vnode.appContext && instance.vnode.appContext.provides
62-
: instance.parent.provides
63-
: currentApp!._context.provides
59+
// #11488, in a nested createApp, prioritize using the provides from currentApp
60+
const provides = currentApp
61+
? currentApp._context.provides
62+
: instance
63+
? instance.parent == null
64+
? instance.vnode.appContext && instance.vnode.appContext.provides
65+
: instance.parent.provides
66+
: undefined
6467

6568
if (provides && (key as string | symbol) in provides) {
6669
// TS doesn't allow symbol as index type

0 commit comments

Comments
 (0)
Please sign in to comment.