]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(options): data options should preserve original object if possible
authorEvan You <yyx990803@gmail.com>
Wed, 2 Oct 2019 14:03:43 +0000 (10:03 -0400)
committerEvan You <yyx990803@gmail.com>
Wed, 2 Oct 2019 14:03:43 +0000 (10:03 -0400)
packages/runtime-core/src/apiApp.ts
packages/runtime-core/src/apiCreateComponent.ts
packages/runtime-core/src/apiLifecycle.ts
packages/runtime-core/src/apiOptions.ts [moved from packages/runtime-core/src/componentOptions.ts with 95% similarity]
packages/runtime-core/src/component.ts
packages/runtime-core/src/componentProxy.ts [moved from packages/runtime-core/src/componentPublicInstanceProxy.ts with 98% similarity]
packages/runtime-core/src/createRenderer.ts
packages/runtime-core/src/directives.ts
packages/runtime-core/src/h.ts
packages/runtime-core/src/index.ts

index 6a932822a9fa8873c13085422b2b6137904ad5b8..584362bee4df145fa72fc8abec429a305479c395 100644 (file)
@@ -1,6 +1,6 @@
 import { Component, Data, ComponentInternalInstance } from './component'
-import { ComponentOptions } from './componentOptions'
-import { ComponentPublicInstance } from './componentPublicInstanceProxy'
+import { ComponentOptions } from './apiOptions'
+import { ComponentPublicInstance } from './componentProxy'
 import { Directive } from './directives'
 import { RootRenderFunction } from './createRenderer'
 import { InjectionKey } from './apiInject'
index a35ea76628e3b50ea820cf941b94b0e30cd92db6..45a3ca6d73b8017ce1546ca314151f18fbefc624 100644 (file)
@@ -4,10 +4,10 @@ import {
   ComponentOptionsWithoutProps,
   ComponentOptionsWithArrayProps,
   ComponentOptionsWithProps
-} from './componentOptions'
+} from './apiOptions'
 import { SetupContext } from './component'
 import { VNodeChild } from './vnode'
-import { ComponentPublicInstance } from './componentPublicInstanceProxy'
+import { ComponentPublicInstance } from './componentProxy'
 import { ExtractPropTypes } from './componentProps'
 import { isFunction } from '@vue/shared'
 
index 39782388044d4e41d597f0843c617523c923324c..833d902c00b26f30a06733d3be293be5f4502c75 100644 (file)
@@ -4,7 +4,7 @@ import {
   currentInstance,
   setCurrentInstance
 } from './component'
-import { ComponentPublicInstance } from './componentPublicInstanceProxy'
+import { ComponentPublicInstance } from './componentProxy'
 import { callWithAsyncErrorHandling, ErrorTypeStrings } from './errorHandling'
 import { warn } from './warning'
 import { capitalize } from '@vue/shared'
similarity index 95%
rename from packages/runtime-core/src/componentOptions.ts
rename to packages/runtime-core/src/apiOptions.ts
index 9ee96ed65380faf65219cf0afc32ab39fe7f9aa9..8f8582e33d178f15773fa84292b4dc1228503b02 100644 (file)
@@ -30,7 +30,8 @@ import { DebuggerEvent, reactive } from '@vue/reactivity'
 import { ComponentPropsOptions, ExtractPropTypes } from './componentProps'
 import { Directive } from './directives'
 import { VNodeChild } from './vnode'
-import { ComponentPublicInstance } from './componentPublicInstanceProxy'
+import { ComponentPublicInstance } from './componentProxy'
+import { warn } from './warning'
 
 interface ComponentOptionsBase<
   Props,
@@ -231,11 +232,15 @@ export function applyOptions(
 
   // state options
   if (dataOptions) {
-    const data =
-      instance.data === EMPTY_OBJ
-        ? (instance.data = reactive({}))
-        : instance.data
-    extend(data, isFunction(dataOptions) ? dataOptions.call(ctx) : dataOptions)
+    const data = isFunction(dataOptions) ? dataOptions.call(ctx) : dataOptions
+    if (!isObject(data)) {
+      __DEV__ && warn(`data() should return an object.`)
+    } else if (instance.data === EMPTY_OBJ) {
+      instance.data = reactive(data)
+    } else {
+      // existing data: this is a mixin or extends.
+      extend(instance.data, data)
+    }
   }
   if (computedOptions) {
     for (const key in computedOptions) {
index d99a636f8d9168cac697043080bd8b67d83373e2..570092dc51080c88260e9b947577e0fff9a48260 100644 (file)
@@ -3,7 +3,7 @@ import { ReactiveEffect, reactive, readonly } from '@vue/reactivity'
 import {
   PublicInstanceProxyHandlers,
   ComponentPublicInstance
-} from './componentPublicInstanceProxy'
+} from './componentProxy'
 import { ComponentPropsOptions } from './componentProps'
 import { Slots } from './componentSlots'
 import { warn } from './warning'
@@ -14,7 +14,7 @@ import {
 } from './errorHandling'
 import { AppContext, createAppContext } from './apiApp'
 import { Directive } from './directives'
-import { applyOptions, ComponentOptions } from './componentOptions'
+import { applyOptions, ComponentOptions } from './apiOptions'
 import {
   EMPTY_OBJ,
   isFunction,
similarity index 98%
rename from packages/runtime-core/src/componentPublicInstanceProxy.ts
rename to packages/runtime-core/src/componentProxy.ts
index 5741eaedfc20a3ffde658689d2b394eb0a06ab05..05f784287164489ca2d15dfad7562d23ae9d49eb 100644 (file)
@@ -2,7 +2,7 @@ import { ComponentInternalInstance, Data } from './component'
 import { nextTick } from './scheduler'
 import { instanceWatch } from './apiWatch'
 import { EMPTY_OBJ, hasOwn } from '@vue/shared'
-import { ExtracComputedReturns } from './componentOptions'
+import { ExtracComputedReturns } from './apiOptions'
 import { UnwrapRef } from '@vue/reactivity'
 
 // public properties exposed on the proxy, which is used as the render context
index 945a8b8e07fa82cbe6bacc5bfc59e8c57e4b9cab..4fc55aed8abd42f72f56851df7e7de014e85bffe 100644 (file)
@@ -42,7 +42,7 @@ import { resolveSlots } from './componentSlots'
 import { ShapeFlags } from './shapeFlags'
 import { pushWarningContext, popWarningContext, warn } from './warning'
 import { invokeDirectiveHook } from './directives'
-import { ComponentPublicInstance } from './componentPublicInstanceProxy'
+import { ComponentPublicInstance } from './componentProxy'
 import { App, createAppAPI } from './apiApp'
 import {
   SuspenseBoundary,
index 2119bec9d7d996dd440deec84b87b9250148c76e..39aa7d6e7d74e0dd1b19b2cf8aacdebfa72327f0 100644 (file)
@@ -17,7 +17,7 @@ import { warn } from './warning'
 import { ComponentInternalInstance } from './component'
 import { currentRenderingInstance } from './componentRenderUtils'
 import { callWithAsyncErrorHandling, ErrorCodes } from './errorHandling'
-import { ComponentPublicInstance } from './componentPublicInstanceProxy'
+import { ComponentPublicInstance } from './componentProxy'
 
 export interface DirectiveBinding {
   instance: ComponentPublicInstance | null
index fbb43c13d5be2fd51a7ab85c74f862c094edecb9..6c9c8c9bd8bfac243769ae0bda0ead7b83801a00 100644 (file)
@@ -15,7 +15,7 @@ import {
   ComponentOptionsWithArrayProps,
   ComponentOptionsWithProps,
   ComponentOptions
-} from './componentOptions'
+} from './apiOptions'
 import { ExtractPropTypes } from './componentProps'
 
 // `h` is a more user-friendly version of `createVNode` that allows omitting the
index 9a7198d249cb5152fffbe7a1cb4346a1e3a5075e..d3a8374a9689e241fcbcda9b1ceef19af850f345 100644 (file)
@@ -64,9 +64,9 @@ export {
   ComponentOptionsWithoutProps,
   ComponentOptionsWithProps,
   ComponentOptionsWithArrayProps
-} from './componentOptions'
+} from './apiOptions'
 
-export { ComponentPublicInstance } from './componentPublicInstanceProxy'
+export { ComponentPublicInstance } from './componentProxy'
 export { RendererOptions } from './createRenderer'
 export { Slot, Slots } from './componentSlots'
 export { Prop, PropType, ComponentPropsOptions } from './componentProps'