]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(runtime-core): implement RFC-0020
authorEvan You <yyx990803@gmail.com>
Thu, 12 Mar 2020 20:13:12 +0000 (16:13 -0400)
committerEvan You <yyx990803@gmail.com>
Thu, 12 Mar 2020 20:13:12 +0000 (16:13 -0400)
BREAKING CHANGE: data no longer supports object format (per RFC-0020)

packages/runtime-core/__tests__/apiOptions.spec.ts
packages/runtime-core/src/apiOptions.ts
packages/vue/__tests__/index.spec.ts

index 36f3617401a2abcf29628bfd592012df1d97d17f..836373c7053506bfdff7fc946bc92db798f20b4d 100644 (file)
@@ -634,9 +634,9 @@ describe('api: options', () => {
     test('data property is already declared in props', () => {
       const Comp = {
         props: { foo: Number },
-        data: {
+        data: () => ({
           foo: 1
-        },
+        }),
         render() {}
       }
 
@@ -649,9 +649,9 @@ describe('api: options', () => {
 
     test('computed property is already declared in data', () => {
       const Comp = {
-        data: {
+        data: () => ({
           foo: 1
-        },
+        }),
         computed: {
           foo() {}
         },
@@ -699,9 +699,9 @@ describe('api: options', () => {
 
     test('methods property is already declared in data', () => {
       const Comp = {
-        data: {
+        data: () => ({
           foo: 2
-        },
+        }),
         methods: {
           foo() {}
         },
index ab0749f72e11fc0c6d43622292c34c6461eed0c6..8769ec30dbd4450096404a2a1650452793c40d0b 100644 (file)
@@ -167,7 +167,7 @@ export interface LegacyOptions<
   // Limitation: we cannot expose RawBindings on the `this` context for data
   // since that leads to some sort of circular inference and breaks ThisType
   // for the entire component.
-  data?: D | ((this: ComponentPublicInstance<Props>) => D)
+  data?: (this: ComponentPublicInstance<Props>) => D
   computed?: C
   methods?: M
   watch?: ComponentWatchOptions
@@ -280,7 +280,13 @@ export function applyOptions(
 
   // state options
   if (dataOptions) {
-    const data = isFunction(dataOptions) ? dataOptions.call(ctx) : dataOptions
+    if (__DEV__ && !isFunction(dataOptions)) {
+      warn(
+        `The data option must be a function. ` +
+          `Plain object usage is no longer supported.`
+      )
+    }
+    const data = dataOptions.call(ctx)
     if (!isObject(data)) {
       __DEV__ && warn(`data() should return an object.`)
     } else if (instance.data === EMPTY_OBJ) {
index 3bde76341e856743e825352b98e250ad7fc4b55b..eb38d9d1868a61b1cf21b7d7036536a557f3f77e 100644 (file)
@@ -89,9 +89,9 @@ describe('compiler + runtime integration', () => {
 
   it('should support using element innerHTML as template', () => {
     const app = createApp({
-      data: {
+      data: () => ({
         msg: 'hello'
-      }
+      })
     })
     const container = document.createElement('div')
     container.innerHTML = '{{msg}}'