在vuejs中,el的作用為:指明Vue實(shí)例的掛載目標(biāo),會(huì)提供一個(gè)在頁(yè)面上已存在的DOM元素作為Vue實(shí)例的掛載目標(biāo);在實(shí)例掛載之后,元素可以使用“vm.$el”訪問(wèn)。
本教程操作環(huán)境:windows7系統(tǒng)、vue2.9.6版,DELL G3電腦。
vue官方API文檔中,對(duì)el有如下描述:
https://cn.vuejs.org/v2/api/#el
el 的作用大家都知道,用于指明 Vue 實(shí)例的掛載目標(biāo)。我們重點(diǎn)關(guān)注上面兩個(gè)紅色嘆號(hào)部分,總結(jié)一下就是:如果存在 render 函數(shù)或 template 屬性,則掛載元素會(huì)被 Vue 生成的 DOM 替換;否則,掛載元素所在的 HTML 會(huì)被提取出來(lái)用作模版
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <div id="ppp"></div> </body> </html>
示例1: render 函數(shù)渲染的 DOM 替換 <p id="ppp"></p>
new Vue({ el: '#ppp', router, store, render: h => h(App) })
示例2:字符串模版替換 <p id="ppp"></p>
new Vue({ el: '#ppp', router, components: { App }, template: '<App/>' })
示例3:手動(dòng)掛載且不存在render函數(shù)和template屬性
new Vue({ router, store, }).$mount('#ppp')