2
0
Эх сурвалжийг харах

fix(inject): allow default value to be `undefined` (#894)

Close #892
Eduardo San Martin Morote 6 жил өмнө
parent
commit
94562daea7

+ 23 - 0
packages/runtime-core/__tests__/apiInject.spec.ts

@@ -284,4 +284,27 @@ describe('api: provide/inject', () => {
     expect(serialize(root)).toBe(`<div><!----></div>`)
     expect(serialize(root)).toBe(`<div><!----></div>`)
     expect(`injection "foo" not found.`).toHaveBeenWarned()
     expect(`injection "foo" not found.`).toHaveBeenWarned()
   })
   })
+
+  it('should not warn when default value is undefined', () => {
+    const Provider = {
+      setup() {
+        return () => h(Middle)
+      }
+    }
+
+    const Middle = {
+      render: () => h(Consumer)
+    }
+
+    const Consumer = {
+      setup() {
+        const foo = inject('foo', undefined)
+        return () => foo
+      }
+    }
+
+    const root = nodeOps.createElement('div')
+    render(h(Provider), root)
+    expect(`injection "foo" not found.`).not.toHaveBeenWarned()
+  })
 })
 })

+ 1 - 1
packages/runtime-core/src/apiInject.ts

@@ -40,7 +40,7 @@ export function inject(
     if (key in provides) {
     if (key in provides) {
       // TS doesn't allow symbol as index type
       // TS doesn't allow symbol as index type
       return provides[key as string]
       return provides[key as string]
-    } else if (defaultValue !== undefined) {
+    } else if (arguments.length > 1) {
       return defaultValue
       return defaultValue
     } else if (__DEV__) {
     } else if (__DEV__) {
       warn(`injection "${String(key)}" not found.`)
       warn(`injection "${String(key)}" not found.`)