File tree 2 files changed +24
-2
lines changed
2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change @@ -303,4 +303,19 @@ describe('api: provide/inject', () => {
303
303
render ( h ( Provider ) , root )
304
304
expect ( `injection "foo" not found.` ) . not . toHaveBeenWarned ( )
305
305
} )
306
+
307
+ // #2400
308
+ it ( 'should not self-inject' , ( ) => {
309
+ const Comp = {
310
+ setup ( ) {
311
+ provide ( 'foo' , 'foo' )
312
+ const injection = inject ( 'foo' , null )
313
+ return ( ) => injection
314
+ }
315
+ }
316
+
317
+ const root = nodeOps . createElement ( 'div' )
318
+ render ( h ( Comp ) , root )
319
+ expect ( serialize ( root ) ) . toBe ( `<div><!----></div>` )
320
+ } )
306
321
} )
Original file line number Diff line number Diff line change @@ -47,8 +47,15 @@ export function inject(
47
47
// a functional component
48
48
const instance = currentInstance || currentRenderingInstance
49
49
if ( instance ) {
50
- const provides = instance . provides
51
- if ( ( key as string | symbol ) in provides ) {
50
+ // #2400
51
+ // to support `app.use` plugins,
52
+ // fallback to appContext's `provides` if the intance is at root
53
+ const provides =
54
+ instance . parent == null
55
+ ? instance . vnode . appContext && instance . vnode . appContext . provides
56
+ : instance . parent . provides
57
+
58
+ if ( provides && ( key as string | symbol ) in provides ) {
52
59
// TS doesn't allow symbol as index type
53
60
return provides [ key as string ]
54
61
} else if ( arguments . length > 1 ) {
You can’t perform that action at this time.
0 commit comments