Skip to content

Commit

Permalink
fix(runtime-core): prioritize using the provides from currentApp in n…
Browse files Browse the repository at this point in the history
…ested createApp (#11502)

close #11488
  • Loading branch information
edison1105 committed Aug 7, 2024
1 parent 81351dc commit 7e75de0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
13 changes: 13 additions & 0 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,25 @@ describe('api: createApp', () => {
const app = createApp({
setup() {
provide('foo', 'should not be seen')

// nested createApp
const childApp = createApp({
setup() {
provide('foo', 'foo from child')
},
})

childApp.provide('foo', 2)
expect(childApp.runWithContext(() => inject('foo'))).toBe(2)

return () => h('div')
},
})
app.provide('foo', 1)

expect(app.runWithContext(() => inject('foo'))).toBe(1)
const root = nodeOps.createElement('div')
app.mount(root)

expect(
app.runWithContext(() => {
Expand Down
13 changes: 8 additions & 5 deletions packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,14 @@ export function inject(
// #2400
// to support `app.use` plugins,
// fallback to appContext's `provides` if the instance is at root
const provides = instance
? instance.parent == null
? instance.vnode.appContext && instance.vnode.appContext.provides
: instance.parent.provides
: currentApp!._context.provides
// #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 (provides && (key as string | symbol) in provides) {
// TS doesn't allow symbol as index type
Expand Down

0 comments on commit 7e75de0

Please sign in to comment.