Skip to content

Commit 9e971e0

Browse files
author
ntepluhina
committed
fix: fixed data to be a function
1 parent 7abdd5a commit 9e971e0

File tree

2 files changed

+126
-79
lines changed

2 files changed

+126
-79
lines changed

src/guide/instance.md

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,59 +29,65 @@ We'll talk about [the component system](TODO:components.html) in detail later. F
2929

3030
## Data and Methods
3131

32-
When a Vue instance is created, it adds all the properties found in its `data` object to Vue's **reactivity system**. When the values of those properties change, the view will "react", updating to match the new values.
32+
When a Vue instance is created, it adds all the properties found in its `data` to Vue's **reactivity system**. When the values of those properties change, the view will "react", updating to match the new values.
3333

3434
```js
3535
// Our data object
36-
const data = { a: 1 };
36+
const data = { a: 1 }
3737

3838
// The object is added to a Vue instance
3939
const vm = Vue.createApp().mount({
40-
data: {
41-
a: 1
40+
data() {
41+
return data
4242
}
4343
})
4444

4545
// Getting the property on the instance
4646
// returns the one from the original data
47-
vm.a === data.a; // => true
47+
vm.a === data.a // => true
4848

4949
// Setting the property on the instance
5050
// also affects the original data
51-
vm.a = 2;
52-
data.a; // => 2
51+
vm.a = 2
52+
data.a // => 2
5353
```
5454

5555
When this data changes, the view will re-render. It should be noted that properties in `data` are only **reactive** if they existed when the instance was created. That means if you add a new property, like:
5656

5757
```js
58-
vm.b = "hi";
58+
vm.b = 'hi'
5959
```
6060

6161
Then changes to `b` will not trigger any view updates. If you know you'll need a property later, but it starts out empty or non-existent, you'll need to set some initial value. For example:
6262

6363
```js
64-
data: {
65-
newTodoText: '',
66-
visitCount: 0,
67-
hideCompletedTodos: false,
68-
todos: [],
69-
error: null
64+
data() {
65+
return {
66+
newTodoText: '',
67+
visitCount: 0,
68+
hideCompletedTodos: false,
69+
todos: [],
70+
error: null
71+
}
7072
}
7173
```
7274

7375
The only exception to this being the use of `Object.freeze()`, which prevents existing properties from being changed, which also means the reactivity system can't _track_ changes.
7476

7577
```js
7678
const obj = {
77-
foo: "bar"
78-
};
79+
foo: 'bar'
80+
}
7981

80-
Object.freeze(obj);
82+
Object.freeze(obj)
8183

82-
const vm = Vue.createApp().mount({
83-
data: obj
84-
},'#app'
84+
const vm = Vue.createApp().mount(
85+
{
86+
data() {
87+
return obj
88+
}
89+
},
90+
'#app'
8591
)
8692
```
8793

@@ -96,10 +102,16 @@ const vm = Vue.createApp().mount({
96102
In addition to data properties, Vue instances expose a number of useful instance properties and methods. These are prefixed with `$` to differentiate them from user-defined properties. For example:
97103

98104
```js
99-
const data = { a: 1 };
100-
const vm = Vue.createApp().mount({
101-
data: data
102-
}, '#example');
105+
const vm = Vue.createApp().mount(
106+
{
107+
data() {
108+
return {
109+
a: 1
110+
}
111+
}
112+
},
113+
'#example'
114+
)
103115

104116
vm.$data.a // => 1
105117
```
@@ -115,16 +127,20 @@ Each Vue instance goes through a series of initialization steps when it's create
115127
For example, the [`created`](TODO:../api/#created) hook can be used to run code after an instance is created:
116128

117129
```js
118-
Vue.createApp().mount({
119-
data: {
120-
a: 1
130+
Vue.createApp().mount(
131+
{
132+
data() {
133+
return {
134+
a: 1
135+
}
136+
},
137+
created() {
138+
// `this` points to the vm instance
139+
console.log('a is: ' + this.a) // => "a is: 1"
140+
}
121141
},
122-
created() {
123-
// `this` points to the vm instance
124-
console.log("a is: " + this.a);
125-
}
126-
}, '#app');
127-
// => "a is: 1"
142+
'#app'
143+
)
128144
```
129145

130146
There are also other hooks which will be called at different stages of the instance's lifecycle, such as [`mounted`](TODO:../api/#mounted), [`updated`](TODO:../api/#updated), and [`destroyed`](TODO:../api/#destroyed). All lifecycle hooks are called with their `this` context pointing to the Vue instance invoking it.

0 commit comments

Comments
 (0)