File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -93,6 +93,14 @@ describe('api: createApp', () => {
93
93
app . provide ( 'foo' , 1 )
94
94
app . provide ( 'bar' , 2 )
95
95
96
+ const foo = app . inject ( 'foo' )
97
+ const bar = app . inject ( 'bar' )
98
+ app . inject ( 'non-existant' )
99
+
100
+ expect ( '[Vue warn]: injection "non-existant" not found.' ) . toHaveBeenWarned ( )
101
+ expect ( foo ) . toBe ( 1 )
102
+ expect ( bar ) . toBe ( 2 )
103
+
96
104
const root = nodeOps . createElement ( 'div' )
97
105
app . mount ( root )
98
106
expect ( serializeInner ( root ) ) . toBe ( `3,2` )
Original file line number Diff line number Diff line change @@ -41,6 +41,7 @@ export interface App<HostElement = any> {
41
41
) : ComponentPublicInstance
42
42
unmount ( ) : void
43
43
provide < T > ( key : InjectionKey < T > | string , value : T ) : this
44
+ inject < T > ( key : InjectionKey < T > | string ) : T | undefined
44
45
45
46
// internal, but we need to expose these for the server-renderer and devtools
46
47
_uid : number
@@ -346,6 +347,14 @@ export function createAppAPI<HostElement>(
346
347
context . provides [ key as string ] = value
347
348
348
349
return app
350
+ } ,
351
+
352
+ inject < T > ( key : InjectionKey < T > | string ) {
353
+ const value = context . provides [ key as string ]
354
+ if ( __DEV__ && ! ( ( key as string ) in context . provides ) ) {
355
+ warn ( `injection "${ String ( key ) } " not found.` )
356
+ }
357
+ return value
349
358
}
350
359
} )
351
360
You can’t perform that action at this time.
0 commit comments