Skip to content

Commit

Permalink
feat(runtime-core): implement RFC-0020
Browse files Browse the repository at this point in the history
BREAKING CHANGE: data no longer supports object format (per RFC-0020)
  • Loading branch information
yyx990803 committed Mar 12, 2020
1 parent dd17fa1 commit bb7fa3d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
12 changes: 6 additions & 6 deletions packages/runtime-core/__tests__/apiOptions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}

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

test('computed property is already declared in data', () => {
const Comp = {
data: {
data: () => ({
foo: 1
},
}),
computed: {
foo() {}
},
Expand Down Expand Up @@ -699,9 +699,9 @@ describe('api: options', () => {

test('methods property is already declared in data', () => {
const Comp = {
data: {
data: () => ({
foo: 2
},
}),
methods: {
foo() {}
},
Expand Down
10 changes: 8 additions & 2 deletions packages/runtime-core/src/apiOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions packages/vue/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}}'
Expand Down

0 comments on commit bb7fa3d

Please sign in to comment.