[Vue]实例化Vue时的两种挂载方式el与$mount

时间:2023-03-09 19:40:17
[Vue]实例化Vue时的两种挂载方式el与$mount

Vue 的$mount()为手动挂载,在项目中可用于延时挂载(例如在挂载之前要进行一些其他操作、判断等),之后要手动挂载上。new Vue时,el和$mount并没有本质上的不同。

1.el

Vue实例:

new Vue({
el: '#app',
data: obj
})

模板:

<div id="app">
<p>{{ foo }}</p>
<!-- 这里的 `foo` 不会更新! -->
<button v-on:click="foo = 'baz'">Change it</button>
</div>

2.$mount

Vue实例:

const app = new Vue({
router,
i18n,
...App
}).$mount('#app')

模板:

<div id="app">
<h1 style="font-size: 16px; text-align: center;">{{ $t("message.hello") }}</h1>
</div>