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
Copy file name to clipboardExpand all lines: src/guide/instance.md
+49-33Lines changed: 49 additions & 33 deletions
Original file line number
Diff line number
Diff line change
@@ -29,59 +29,65 @@ We'll talk about [the component system](TODO:components.html) in detail later. F
29
29
30
30
## Data and Methods
31
31
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.
33
33
34
34
```js
35
35
// Our data object
36
-
constdata= { a:1 };
36
+
constdata= { a:1 }
37
37
38
38
// The object is added to a Vue instance
39
39
constvm=Vue.createApp().mount({
40
-
data: {
41
-
a:1
40
+
data() {
41
+
return data
42
42
}
43
43
})
44
44
45
45
// Getting the property on the instance
46
46
// returns the one from the original data
47
-
vm.a===data.a;// => true
47
+
vm.a===data.a// => true
48
48
49
49
// Setting the property on the instance
50
50
// also affects the original data
51
-
vm.a=2;
52
-
data.a;// => 2
51
+
vm.a=2
52
+
data.a// => 2
53
53
```
54
54
55
55
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:
56
56
57
57
```js
58
-
vm.b="hi";
58
+
vm.b='hi'
59
59
```
60
60
61
61
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:
62
62
63
63
```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
+
}
70
72
}
71
73
```
72
74
73
75
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.
74
76
75
77
```js
76
78
constobj= {
77
-
foo:"bar"
78
-
};
79
+
foo:'bar'
80
+
}
79
81
80
-
Object.freeze(obj);
82
+
Object.freeze(obj)
81
83
82
-
constvm=Vue.createApp().mount({
83
-
data: obj
84
-
},'#app'
84
+
constvm=Vue.createApp().mount(
85
+
{
86
+
data() {
87
+
return obj
88
+
}
89
+
},
90
+
'#app'
85
91
)
86
92
```
87
93
@@ -96,10 +102,16 @@ const vm = Vue.createApp().mount({
96
102
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:
97
103
98
104
```js
99
-
constdata= { a:1 };
100
-
constvm=Vue.createApp().mount({
101
-
data: data
102
-
}, '#example');
105
+
constvm=Vue.createApp().mount(
106
+
{
107
+
data() {
108
+
return {
109
+
a:1
110
+
}
111
+
}
112
+
},
113
+
'#example'
114
+
)
103
115
104
116
vm.$data.a// => 1
105
117
```
@@ -115,16 +127,20 @@ Each Vue instance goes through a series of initialization steps when it's create
115
127
For example, the [`created`](TODO:../api/#created) hook can be used to run code after an instance is created:
116
128
117
129
```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
+
}
121
141
},
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
+
)
128
144
```
129
145
130
146
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