Skip to content

Commit 39c7432

Browse files
author
ntepluhina
committed
fix: fixed instance doc
1 parent d655410 commit 39c7432

File tree

1 file changed

+37
-37
lines changed

1 file changed

+37
-37
lines changed

src/guide/instance.md

Lines changed: 37 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,13 @@
55
Every Vue application starts by creating a new **Vue instance** with the `createApp` function:
66

77
```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");
915
```
1016

1117
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
3339

3440
```js
3541
// Our data object
36-
const data = { a: 1 }
42+
const data = { a: 1 };
3743

3844
// The object is added to a Vue instance
39-
const vm = Vue.createApp().mount({
45+
const vm = Vue.createApp({
4046
data() {
41-
return data
47+
return data;
4248
}
43-
})
49+
}).mount("#app");
4450

4551
// Getting the property on the instance
4652
// returns the one from the original data
47-
vm.a === data.a // => true
53+
vm.a === data.a; // => true
4854

4955
// Setting the property on the instance
5056
// also affects the original data
51-
vm.a = 2
52-
data.a // => 2
57+
vm.a = 2;
58+
data.a; // => 2
5359
```
5460

5561
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:
5662

5763
```js
58-
vm.b = 'hi'
64+
vm.b = "hi";
5965
```
6066

6167
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
7682

7783
```js
7884
const obj = {
79-
foo: 'bar'
80-
}
85+
foo: "bar"
86+
};
8187

82-
Object.freeze(obj)
88+
Object.freeze(obj);
8389

84-
const vm = Vue.createApp().mount(
85-
{
86-
data() {
87-
return obj
88-
}
89-
},
90-
'#app'
91-
)
90+
const vm = Vue.createApp({
91+
data() {
92+
return obj;
93+
}
94+
}).mount("#app");
9295
```
9396

9497
```html
@@ -107,13 +110,13 @@ const vm = Vue.createApp().mount(
107110
data() {
108111
return {
109112
a: 1
110-
}
113+
};
111114
}
112115
},
113-
'#example'
114-
)
116+
"#example"
117+
);
115118

116-
vm.$data.a // => 1
119+
vm.$data.a; // => 1
117120
```
118121

119122
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
127130
For example, the [`created`](TODO:../api/#created) hook can be used to run code after an instance is created:
128131

129132
```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+
};
141138
},
142-
'#app'
143-
)
139+
created() {
140+
// `this` points to the vm instance
141+
console.log("a is: " + this.a); // => "a is: 1"
142+
}
143+
});
144144
```
145145

146146
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)