Skip to content

Commit 637ec75

Browse files
committed
fead: add migration guide > mount changes
1 parent 5c63b27 commit 637ec75

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

src/guide/migration/mount-changes.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: 'Mount API changes'
3+
badges:
4+
- breaking
5+
---
6+
7+
# Mounted application does not replace the element <MigrationBadges :badges="$frontmatter.badges" />
8+
9+
## Overview
10+
11+
In Vue 2.x, when mounting an application that has a `template`, the rendered content replaces the element we mount to. In Vue 3.x, the rendered application is appended as a child of such an element, replacing element's `innerHTML`.
12+
13+
## 2.x Syntax
14+
15+
In Vue 2.x, we pass an HTML element selector to `new Vue()` or `$mount`:
16+
17+
```js
18+
new Vue({
19+
el: '#app',
20+
data() {
21+
return {
22+
message: 'Hello Vue!'
23+
}
24+
},
25+
template: `
26+
<div id="rendered">{{ message }}</div>
27+
`
28+
})
29+
30+
// or
31+
const app = new Vue({
32+
data() {
33+
return {
34+
message: 'Hello Vue!'
35+
}
36+
},
37+
template: `
38+
<div id="rendered">{{ message }}</div>
39+
`
40+
})
41+
42+
app.$mount('#app')
43+
```
44+
45+
When we mount this application to the page that has a `div` with the passed selector (in our case, it's `id="app"`):
46+
47+
```html
48+
<body>
49+
<div id="app">
50+
Some app content
51+
</div>
52+
</body>
53+
```
54+
55+
in the rendered result, the mentioned `div` will be replaced with the rendered application content:
56+
57+
```html
58+
<body>
59+
<div id="rendered">Hello Vue!</div>
60+
</body>
61+
```
62+
63+
## 3.x Syntax
64+
65+
In Vue 3.x, when we mount an application, its rendered content will replace the `innerHTML` of the element we pass to `mount`:
66+
67+
```js
68+
const app = Vue.createApp({
69+
data() {
70+
return {
71+
message: 'Hello Vue!'
72+
}
73+
},
74+
template: `
75+
<div id="rendered">{{ message }}</div>
76+
`
77+
})
78+
79+
app.mount('#app')
80+
```
81+
82+
When this app is mounted to the page that has a `div` with `id="app"`, this will result in:
83+
84+
```html
85+
<body>
86+
<div id="app" data-v-app="">
87+
<div id="rendered">Hello Vue!</div>
88+
</div>
89+
</body>
90+
```
91+
92+
## See also
93+
94+
- [`mount` API](/api/application-api.html#mount)

0 commit comments

Comments
 (0)