Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(runtime-core): fix inject with currentApp #11604

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/runtime-core/__tests__/apiCreateApp.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
20 changes: 13 additions & 7 deletions packages/runtime-core/src/apiInject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should move this code above const provides = instance

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
Expand Down