|
|
@@ -1,6 +1,7 @@
|
|
|
import { isFunction } from '@vue/shared'
|
|
|
import { currentInstance } from './component'
|
|
|
import { currentRenderingInstance } from './componentRenderContext'
|
|
|
+import { currentApp } from './apiCreateApp'
|
|
|
import { warn } from './warning'
|
|
|
|
|
|
export interface InjectionKey<T> extends Symbol {}
|
|
|
@@ -46,21 +47,24 @@ export function inject(
|
|
|
// fallback to `currentRenderingInstance` so that this can be called in
|
|
|
// a functional component
|
|
|
const instance = currentInstance || currentRenderingInstance
|
|
|
- if (instance) {
|
|
|
+
|
|
|
+ // also support looking up from app-level provides w/ `app.runWithContext()`
|
|
|
+ if (instance || currentApp) {
|
|
|
// #2400
|
|
|
// to support `app.use` plugins,
|
|
|
// fallback to appContext's `provides` if the instance is at root
|
|
|
- const provides =
|
|
|
- instance.parent == null
|
|
|
+ const provides = instance
|
|
|
+ ? instance.parent == null
|
|
|
? instance.vnode.appContext && instance.vnode.appContext.provides
|
|
|
: instance.parent.provides
|
|
|
+ : currentApp!._context.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) {
|
|
|
return treatDefaultAsFactory && isFunction(defaultValue)
|
|
|
- ? defaultValue.call(instance.proxy)
|
|
|
+ ? defaultValue.call(instance && instance.proxy)
|
|
|
: defaultValue
|
|
|
} else if (__DEV__) {
|
|
|
warn(`injection "${String(key)}" not found.`)
|