Skip to content

Commit bb7fa3d

Browse files
committed
feat(runtime-core): implement RFC-0020
BREAKING CHANGE: data no longer supports object format (per RFC-0020)
1 parent dd17fa1 commit bb7fa3d

File tree

3 files changed

+16
-10
lines changed

3 files changed

+16
-10
lines changed

packages/runtime-core/__tests__/apiOptions.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -634,9 +634,9 @@ describe('api: options', () => {
634634
test('data property is already declared in props', () => {
635635
const Comp = {
636636
props: { foo: Number },
637-
data: {
637+
data: () => ({
638638
foo: 1
639-
},
639+
}),
640640
render() {}
641641
}
642642

@@ -649,9 +649,9 @@ describe('api: options', () => {
649649

650650
test('computed property is already declared in data', () => {
651651
const Comp = {
652-
data: {
652+
data: () => ({
653653
foo: 1
654-
},
654+
}),
655655
computed: {
656656
foo() {}
657657
},
@@ -699,9 +699,9 @@ describe('api: options', () => {
699699

700700
test('methods property is already declared in data', () => {
701701
const Comp = {
702-
data: {
702+
data: () => ({
703703
foo: 2
704-
},
704+
}),
705705
methods: {
706706
foo() {}
707707
},

packages/runtime-core/src/apiOptions.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export interface LegacyOptions<
167167
// Limitation: we cannot expose RawBindings on the `this` context for data
168168
// since that leads to some sort of circular inference and breaks ThisType
169169
// for the entire component.
170-
data?: D | ((this: ComponentPublicInstance<Props>) => D)
170+
data?: (this: ComponentPublicInstance<Props>) => D
171171
computed?: C
172172
methods?: M
173173
watch?: ComponentWatchOptions
@@ -280,7 +280,13 @@ export function applyOptions(
280280

281281
// state options
282282
if (dataOptions) {
283-
const data = isFunction(dataOptions) ? dataOptions.call(ctx) : dataOptions
283+
if (__DEV__ && !isFunction(dataOptions)) {
284+
warn(
285+
`The data option must be a function. ` +
286+
`Plain object usage is no longer supported.`
287+
)
288+
}
289+
const data = dataOptions.call(ctx)
284290
if (!isObject(data)) {
285291
__DEV__ && warn(`data() should return an object.`)
286292
} else if (instance.data === EMPTY_OBJ) {

packages/vue/__tests__/index.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ describe('compiler + runtime integration', () => {
8989

9090
it('should support using element innerHTML as template', () => {
9191
const app = createApp({
92-
data: {
92+
data: () => ({
9393
msg: 'hello'
94-
}
94+
})
9595
})
9696
const container = document.createElement('div')
9797
container.innerHTML = '{{msg}}'

0 commit comments

Comments
 (0)