wip: props default this compat

This commit is contained in:
Evan You 2021-04-06 11:08:21 -04:00
parent 65cc649559
commit d619a770a8
3 changed files with 28 additions and 3 deletions

View File

@ -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`
}
}

View File

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

View File

@ -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 {