]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(runtime-core): default value for function type prop (#1349)
authortangjinzhou <415800467@qq.com>
Fri, 12 Jun 2020 13:06:28 +0000 (21:06 +0800)
committerGitHub <noreply@github.com>
Fri, 12 Jun 2020 13:06:28 +0000 (09:06 -0400)
fix #1348

packages/runtime-core/__tests__/componentProps.spec.ts
packages/runtime-core/src/componentProps.ts

index 59167ea53513b8bd6df391539053575b912e86f4..fb701d2bd2f772985c34dd5116da4012b32edac5 100644 (file)
@@ -158,6 +158,7 @@ describe('component props', () => {
   test('default value', () => {
     let proxy: any
     const defaultFn = jest.fn(() => ({ a: 1 }))
+    const defaultBaz = jest.fn(() => ({ b: 1 }))
 
     const Comp = {
       props: {
@@ -166,6 +167,10 @@ describe('component props', () => {
         },
         bar: {
           default: defaultFn
+        },
+        baz: {
+          type: Function,
+          default: defaultBaz
         }
       },
       render() {
@@ -178,7 +183,9 @@ describe('component props', () => {
     expect(proxy.foo).toBe(2)
     const prevBar = proxy.bar
     expect(proxy.bar).toEqual({ a: 1 })
+    expect(proxy.baz).toEqual(defaultBaz)
     expect(defaultFn).toHaveBeenCalledTimes(1)
+    expect(defaultBaz).toHaveBeenCalledTimes(0)
 
     // #999: updates should not cause default factory of unchanged prop to be
     // called again
index 5d3cef7e39d42a091698e0c1182061e95e2addfc..0f6fecb14e61d6d311b82a63c77c583d3c0332b4 100644 (file)
@@ -270,13 +270,16 @@ function resolvePropValue(
   key: string,
   value: unknown
 ) {
-  const opt = options[key]
+  const opt = options[key] as any
   if (opt != null) {
     const hasDefault = hasOwn(opt, 'default')
     // default values
     if (hasDefault && value === undefined) {
       const defaultValue = opt.default
-      value = isFunction(defaultValue) ? defaultValue() : defaultValue
+      value =
+        opt.type !== Function && isFunction(defaultValue)
+          ? defaultValue()
+          : defaultValue
     }
     // boolean casting
     if (opt[BooleanFlags.shouldCast]) {