Browse Source

wip: props default this compat

Evan You 5 years ago
parent
commit
d619a770a8

+ 11 - 2
packages/runtime-core/src/compat/deprecations.ts

@@ -22,7 +22,9 @@ export const enum DeprecationTypes {
   OPTIONS_DATA_FN,
   OPTIONS_DATA_MERGE,
   OPTIONS_BEFORE_DESTROY,
-  OPTIONS_DESTROYED
+  OPTIONS_DESTROYED,
+
+  PROPS_DEFAULT_THIS
 }
 
 type DeprecationData = {
@@ -137,7 +139,7 @@ const deprecations: Record<DeprecationTypes, DeprecationData> = {
 
   [DeprecationTypes.OPTIONS_DATA_MERGE]: {
     message: (key: string) =>
-      `Detected conflicting key "${key}" when merging "data" option values. ` +
+      `Detected conflicting key "${key}" when merging data option values. ` +
       `In Vue 3, data keys are merged shallowly and will override one another.`,
     link: `https://v3.vuejs.org/guide/migration/data-option.html#mixin-merge-behavior-change`
   },
@@ -148,6 +150,13 @@ const deprecations: Record<DeprecationTypes, DeprecationData> = {
 
   [DeprecationTypes.OPTIONS_DESTROYED]: {
     message: `\`destroyed\` has been renamed to \`unmounted\`.`
+  },
+
+  [DeprecationTypes.PROPS_DEFAULT_THIS]: {
+    message: (key: string) =>
+      `props default value function no longer has access to "this". ` +
+      `(found in prop "${key}")`,
+    link: `https://v3.vuejs.org/guide/migration/props-default-this.html`
   }
 }
 

+ 12 - 0
packages/runtime-core/src/compat/props.ts

@@ -0,0 +1,12 @@
+import { DeprecationTypes, warnDeprecation } from './deprecations'
+
+export function createPropsDefaultThis(propKey: string) {
+  return new Proxy(
+    {},
+    {
+      get() {
+        warnDeprecation(DeprecationTypes.PROPS_DEFAULT_THIS, propKey)
+      }
+    }
+  )
+}

+ 5 - 1
packages/runtime-core/src/componentProps.ts

@@ -33,6 +33,7 @@ import {
 import { isEmitListener } from './componentEmits'
 import { InternalObjectKey } from './vnode'
 import { AppContext } from './apiCreateApp'
+import { createPropsDefaultThis } from './compat/props'
 
 export type ComponentPropsOptions<P = Data> =
   | ComponentObjectPropsOptions<P>
@@ -342,7 +343,10 @@ function resolvePropValue(
           value = propsDefaults[key]
         } else {
           setCurrentInstance(instance)
-          value = propsDefaults[key] = defaultValue(props)
+          value = propsDefaults[key] =
+            __COMPAT__ && __DEV__
+              ? defaultValue.call(createPropsDefaultThis(key), props)
+              : defaultValue(props)
           setCurrentInstance(null)
         }
       } else {