Skip to content

Commit 4423af4

Browse files
docs (#157): add data object api changes to migration guide (#165)
* docs (#157): add data object api changes to migration guide * docs (#157): improve clarity Co-authored-by: Natalia Tepluhina <[email protected]> * docs (#162): update syntax Co-authored-by: Natalia Tepluhina <[email protected]> * docs (#157): remove misleading tag Co-authored-by: Natalia Tepluhina <[email protected]>
1 parent 7faa42f commit 4423af4

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

src/.vuepress/config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ const sidebar = {
9494
'migration/functional-components',
9595
'migration/async-components',
9696
'migration/custom-directives',
97+
'migration/data-option',
9798
'migration/filters',
9899
'migration/fragments',
99100
'migration/render-function-api',

src/guide/migration/data-option.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
types:
3+
- breaking
4+
---
5+
6+
# Data Option <span v-for="type in $frontmatter.types" class="badge" :key="`type-${type}`">{{ type }}</span>
7+
8+
## Overview
9+
10+
- **BREAKING**: `data` component option declaration no longer accepts a plain JavaScript `object` and expects a `function` declaration.
11+
12+
## 2.x Syntax
13+
14+
In 2.x, developers could define the `data` option with either an `object` or a `function`.
15+
16+
For example:
17+
18+
```html
19+
<!-- Object Declaration -->
20+
<script>
21+
const app = new Vue({
22+
data: {
23+
apiKey: 'a1b2c3'
24+
}
25+
})
26+
</script>
27+
28+
<!-- Function Declaration -->
29+
<script>
30+
const app = new Vue({
31+
data() {
32+
return {
33+
apiKey: 'a1b2c3'
34+
}
35+
}
36+
})
37+
</script>
38+
```
39+
40+
Though this provided some convenience in terms of root instances having a shared state, this has led to confusion due to the fact that its only possible on the root instance.
41+
42+
## 3.x Update
43+
44+
In 3.x, the `data` option has been standardized to only accept a `function` that returns an `object`.
45+
46+
Using the example above, there would only be one possible implementation of the code:
47+
48+
```html
49+
<script>
50+
import { createApp } from 'vue'
51+
52+
createApp({
53+
data() {
54+
return {
55+
apiKey: 'a1b2c3'
56+
}
57+
}
58+
}).mount('#app')
59+
</script>
60+
```
61+
62+
## How to Migrate
63+
64+
For users relying on the object declaration, we recommend:
65+
66+
- Extracting the shared data into an external object and using it as a property in `data`
67+
- Rewrite references to the shared data to point to a new shared object

0 commit comments

Comments
 (0)