@@ -3,7 +3,6 @@ import { currentInstance } from './component'
3
3
import { currentRenderingInstance } from './componentRenderContext'
4
4
import { currentApp } from './apiCreateApp'
5
5
import { warn } from './warning'
6
- import { ComponentPublicInstance } from './componentPublicInstance'
7
6
8
7
export interface InjectionKey < T > extends Symbol { }
9
8
@@ -46,28 +45,26 @@ export function inject(
46
45
treatDefaultAsFactory = false
47
46
) {
48
47
// fallback to `currentRenderingInstance` so that this can be called in
49
- // a functional component and to currentApp so it can be called within `app.runWithContext()`
50
- const instance = currentInstance || currentRenderingInstance || currentApp
48
+ // a functional component
49
+ const instance = currentInstance || currentRenderingInstance
51
50
52
- if ( instance ) {
51
+ // also support looking up from app-level provides w/ `app.runWithContext()`
52
+ if ( instance || currentApp ) {
53
53
// #2400
54
54
// to support `app.use` plugins,
55
55
// fallback to appContext's `provides` if the instance is at root
56
- const provides =
57
- 'mount' in instance // checks if instance is an App
58
- ? instance . _context . provides
59
- : instance . parent == null
56
+ const provides = instance
57
+ ? instance . parent == null
60
58
? instance . vnode . appContext && instance . vnode . appContext . provides
61
59
: instance . parent . provides
60
+ : currentApp ! . _context . provides
62
61
63
62
if ( provides && ( key as string | symbol ) in provides ) {
64
63
// TS doesn't allow symbol as index type
65
64
return provides [ key as string ]
66
65
} else if ( arguments . length > 1 ) {
67
66
return treatDefaultAsFactory && isFunction ( defaultValue )
68
- ? defaultValue . call (
69
- ( instance as { proxy ?: ComponentPublicInstance | null } ) . proxy
70
- )
67
+ ? defaultValue . call ( instance && instance . proxy )
71
68
: defaultValue
72
69
} else if ( __DEV__ ) {
73
70
warn ( `injection "${ String ( key ) } " not found.` )
0 commit comments