You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# Data Option <spanv-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
+
constapp=newVue({
22
+
data: {
23
+
apiKey:'a1b2c3'
24
+
}
25
+
})
26
+
</script>
27
+
28
+
<!-- Function Declaration -->
29
+
<script>
30
+
constapp=newVue({
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