File tree 2 files changed +34
-9
lines changed
2 files changed +34
-9
lines changed Original file line number Diff line number Diff line change @@ -74,13 +74,11 @@ export function inject(
74
74
const val = resolveInject ( key , vm )
75
75
if ( val !== NOT_FOUND ) {
76
76
return val
77
+ } else if ( arguments . length > 1 ) {
78
+ return treatDefaultAsFactory && isFunction ( defaultValue )
79
+ ? defaultValue ( )
80
+ : defaultValue
81
+ } else if ( __DEV__ ) {
82
+ warn ( `Injection "${ String ( key ) } " not found.` , vm )
77
83
}
78
-
79
- if ( defaultValue === undefined && __DEV__ ) {
80
- warn ( `Injection "${ String ( key ) } " not found` , vm )
81
- }
82
-
83
- return treatDefaultAsFactory && isFunction ( defaultValue )
84
- ? defaultValue ( )
85
- : defaultValue
86
84
}
Original file line number Diff line number Diff line change @@ -239,7 +239,7 @@ describe('api: provide/inject', () => {
239
239
const root = document . createElement ( 'div' )
240
240
const vm = createApp ( Provider ) . mount ( root )
241
241
expect ( vm . $el . outerHTML ) . toBe ( `<div></div>` )
242
- expect ( `[Vue warn]: Injection "foo" not found` ) . toHaveBeenWarned ( )
242
+ expect ( `[Vue warn]: Injection "foo" not found. ` ) . toHaveBeenWarned ( )
243
243
} )
244
244
245
245
it ( 'should warn unfound w/ injectionKey is undefined' , ( ) => {
@@ -277,4 +277,31 @@ describe('api: provide/inject', () => {
277
277
const vm = createApp ( Comp ) . mount ( root )
278
278
expect ( vm . $el . outerHTML ) . toBe ( `<div>foo</div>` )
279
279
} )
280
+
281
+ it ( 'should not warn when default value is undefined' , ( ) => {
282
+ const Provider = {
283
+ setup ( ) {
284
+ provide ( 'foo' , undefined )
285
+ return ( ) => h ( Middle )
286
+ } ,
287
+ }
288
+
289
+ const Middle = {
290
+ setup ( ) {
291
+ return ( ) => h ( Consumer )
292
+ } ,
293
+ }
294
+
295
+ const Consumer = {
296
+ setup ( ) {
297
+ const foo = inject ( 'foo' )
298
+ return ( ) => h ( 'div' , foo as unknown as string )
299
+ } ,
300
+ }
301
+
302
+ const root = document . createElement ( 'div' )
303
+ const vm = createApp ( Provider ) . mount ( root )
304
+ expect ( vm . $el . outerHTML ) . toBe ( `<div></div>` )
305
+ expect ( `injection "foo" not found.` ) . not . toHaveBeenWarned ( )
306
+ } )
280
307
} )
You can’t perform that action at this time.
0 commit comments