Просмотр исходного кода

fix(runtome-core): do not cache property access in beforeCreate hook

fix #1756
Evan You 5 лет назад
Родитель
Сommit
f6afe7000e

+ 4 - 0
packages/runtime-core/src/componentOptions.ts

@@ -364,6 +364,8 @@ function createDuplicateChecker() {
 
 
 type DataFn = (vm: ComponentPublicInstance) => any
 type DataFn = (vm: ComponentPublicInstance) => any
 
 
+export let isInBeforeCreate = false
+
 export function applyOptions(
 export function applyOptions(
   instance: ComponentInternalInstance,
   instance: ComponentInternalInstance,
   options: ComponentOptions,
   options: ComponentOptions,
@@ -407,7 +409,9 @@ export function applyOptions(
 
 
   // applyOptions is called non-as-mixin once per instance
   // applyOptions is called non-as-mixin once per instance
   if (!asMixin) {
   if (!asMixin) {
+    isInBeforeCreate = true
     callSyncHook('beforeCreate', options, publicThis, globalMixins)
     callSyncHook('beforeCreate', options, publicThis, globalMixins)
+    isInBeforeCreate = false
     // global mixins are applied first
     // global mixins are applied first
     applyMixins(instance, globalMixins, deferredData, deferredWatch)
     applyMixins(instance, globalMixins, deferredData, deferredWatch)
   }
   }

+ 3 - 2
packages/runtime-core/src/componentProxy.ts

@@ -26,7 +26,8 @@ import {
   ComponentOptionsMixin,
   ComponentOptionsMixin,
   OptionTypesType,
   OptionTypesType,
   OptionTypesKeys,
   OptionTypesKeys,
-  resolveMergedOptions
+  resolveMergedOptions,
+  isInBeforeCreate
 } from './componentOptions'
 } from './componentOptions'
 import { normalizePropsOptions } from './componentProps'
 import { normalizePropsOptions } from './componentProps'
 import { EmitsOptions, EmitFn } from './componentEmits'
 import { EmitsOptions, EmitFn } from './componentEmits'
@@ -254,7 +255,7 @@ export const PublicInstanceProxyHandlers: ProxyHandler<any> = {
       } else if (ctx !== EMPTY_OBJ && hasOwn(ctx, key)) {
       } else if (ctx !== EMPTY_OBJ && hasOwn(ctx, key)) {
         accessCache![key] = AccessTypes.CONTEXT
         accessCache![key] = AccessTypes.CONTEXT
         return ctx[key]
         return ctx[key]
-      } else {
+      } else if (!__FEATURE_OPTIONS_API__ || !isInBeforeCreate) {
         accessCache![key] = AccessTypes.OTHER
         accessCache![key] = AccessTypes.OTHER
       }
       }
     }
     }