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
+37-37Lines changed: 37 additions & 37 deletions
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,13 @@
5
5
Every Vue application starts by creating a new **Vue instance** with the `createApp` function:
6
6
7
7
```js
8
-
Vue.createApp().mount(/* options */)
8
+
Vue.createApp(/* options */);
9
+
```
10
+
11
+
After the Vue instance is created, we can _mount_ it, passing a container to `.mount` method. For example, if we want to mount a Vue application on `<div id="app"></div>`, we should pass `#app`:
12
+
13
+
```js
14
+
Vue.createApp(/* options */).mount("#app");
9
15
```
10
16
11
17
Although not strictly associated with the [MVVM pattern](https://en.wikipedia.org/wiki/Model_View_ViewModel), Vue's design was partly inspired by it. As a convention, we often use the variable `vm` (short for ViewModel) to refer to our Vue instance.
@@ -33,29 +39,29 @@ When a Vue instance is created, it adds all the properties found in its `data` t
33
39
34
40
```js
35
41
// Our data object
36
-
constdata= { a:1 }
42
+
constdata= { a:1 };
37
43
38
44
// The object is added to a Vue instance
39
-
constvm=Vue.createApp().mount({
45
+
constvm=Vue.createApp({
40
46
data() {
41
-
return data
47
+
return data;
42
48
}
43
-
})
49
+
}).mount("#app");
44
50
45
51
// Getting the property on the instance
46
52
// returns the one from the original data
47
-
vm.a===data.a// => true
53
+
vm.a===data.a;// => true
48
54
49
55
// Setting the property on the instance
50
56
// also affects the original data
51
-
vm.a=2
52
-
data.a// => 2
57
+
vm.a=2;
58
+
data.a;// => 2
53
59
```
54
60
55
61
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
62
57
63
```js
58
-
vm.b='hi'
64
+
vm.b="hi";
59
65
```
60
66
61
67
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:
@@ -76,19 +82,16 @@ The only exception to this being the use of `Object.freeze()`, which prevents ex
76
82
77
83
```js
78
84
constobj= {
79
-
foo:'bar'
80
-
}
85
+
foo:"bar"
86
+
};
81
87
82
-
Object.freeze(obj)
88
+
Object.freeze(obj);
83
89
84
-
constvm=Vue.createApp().mount(
85
-
{
86
-
data() {
87
-
return obj
88
-
}
89
-
},
90
-
'#app'
91
-
)
90
+
constvm=Vue.createApp({
91
+
data() {
92
+
return obj;
93
+
}
94
+
}).mount("#app");
92
95
```
93
96
94
97
```html
@@ -107,13 +110,13 @@ const vm = Vue.createApp().mount(
107
110
data() {
108
111
return {
109
112
a:1
110
-
}
113
+
};
111
114
}
112
115
},
113
-
'#example'
114
-
)
116
+
"#example"
117
+
);
115
118
116
-
vm.$data.a// => 1
119
+
vm.$data.a;// => 1
117
120
```
118
121
119
122
In the future, you can consult the [API reference](TODO:../api/#Instance-Properties) for a full list of instance properties and methods.
@@ -127,20 +130,17 @@ Each Vue instance goes through a series of initialization steps when it's create
127
130
For example, the [`created`](TODO:../api/#created) hook can be used to run code after an instance is created:
128
131
129
132
```js
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
-
}
133
+
Vue.createApp({
134
+
data() {
135
+
return {
136
+
a:1
137
+
};
141
138
},
142
-
'#app'
143
-
)
139
+
created() {
140
+
// `this` points to the vm instance
141
+
console.log("a is: "+this.a); // => "a is: 1"
142
+
}
143
+
});
144
144
```
145
145
146
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