]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(runtime-core): support using inject() inside props default functions
authorEvan You <yyx990803@gmail.com>
Thu, 17 Sep 2020 19:59:01 +0000 (15:59 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 17 Sep 2020 19:59:01 +0000 (15:59 -0400)
packages/runtime-core/__tests__/componentProps.spec.ts
packages/runtime-core/src/componentProps.ts

index d0ddc5c1fdc2b00cc693b5816825fd1c1565db40..f75d3c2ebbd419c687c009eb9bc52bcbbc60ad9d 100644 (file)
@@ -8,7 +8,9 @@ import {
   defineComponent,
   ref,
   serializeInner,
-  createApp
+  createApp,
+  provide,
+  inject
 } from '@vue/runtime-test'
 import { render as domRender, nextTick } from 'vue'
 
@@ -212,6 +214,32 @@ describe('component props', () => {
     expect(defaultFn).toHaveBeenCalledTimes(1)
   })
 
+  test('using inject in default value factory', () => {
+    const Child = defineComponent({
+      props: {
+        test: {
+          default: () => inject('test', 'default')
+        }
+      },
+      setup(props) {
+        return () => {
+          return h('div', props.test)
+        }
+      }
+    })
+
+    const Comp = {
+      setup() {
+        provide('test', 'injected')
+        return () => h(Child)
+      }
+    }
+
+    const root = nodeOps.createElement('div')
+    render(h(Comp), root)
+    expect(serializeInner(root)).toBe(`<div>injected</div>`)
+  })
+
   test('optimized props updates', async () => {
     const Child = defineComponent({
       props: ['foo'],
index 0fde127a28f76249369f1bbb012203e3a8daa554..f977099d8063dab6d9ea2952dcc868d00a7173c1 100644 (file)
@@ -27,7 +27,8 @@ import {
   Data,
   ComponentInternalInstance,
   ComponentOptions,
-  ConcreteComponent
+  ConcreteComponent,
+  setCurrentInstance
 } from './component'
 import { isEmitListener } from './componentEmits'
 import { InternalObjectKey } from './vnode'
@@ -179,7 +180,8 @@ export function updateProps(
               options,
               rawCurrentProps,
               camelizedKey,
-              value
+              value,
+              instance
             )
           }
         } else {
@@ -214,7 +216,8 @@ export function updateProps(
               options,
               rawProps || EMPTY_OBJ,
               key,
-              undefined
+              undefined,
+              instance
             )
           }
         } else {
@@ -277,7 +280,8 @@ function setFullProps(
         options!,
         rawCurrentProps,
         key,
-        rawCurrentProps[key]
+        rawCurrentProps[key],
+        instance
       )
     }
   }
@@ -287,7 +291,8 @@ function resolvePropValue(
   options: NormalizedProps,
   props: Data,
   key: string,
-  value: unknown
+  value: unknown,
+  instance: ComponentInternalInstance
 ) {
   const opt = options[key]
   if (opt != null) {
@@ -295,10 +300,13 @@ function resolvePropValue(
     // default values
     if (hasDefault && value === undefined) {
       const defaultValue = opt.default
-      value =
-        opt.type !== Function && isFunction(defaultValue)
-          ? defaultValue(props)
-          : defaultValue
+      if (opt.type !== Function && isFunction(defaultValue)) {
+        setCurrentInstance(instance)
+        value = defaultValue(props)
+        setCurrentInstance(null)
+      } else {
+        value = defaultValue
+      }
     }
     // boolean casting
     if (opt[BooleanFlags.shouldCast]) {