在項目中,你可能遇到這樣的問題,使用別人封裝的框架中的某個組件,但是你無法改動,但是想往組件里面添加一點自己的東西,這個時候你可能需要用到Vue的掛載,下面僅作學(xué)習(xí),不正確之處,還望見諒...
舉個栗子,比如別人封裝了一個菜單組件,你無法修改,但是你的領(lǐng)導(dǎo)突然想在菜單欄中增加一個檢索功能,可以模糊搜索菜單,點擊跳轉(zhuǎn),我們就可以手動掛載,下面就將演示掛載的寫法:
var MyComponent = Vue.extend({
template: '<div>Hello!</div>'
})
// Vue.extend()并非只能使用模板字符串這種寫法,也可以使用單文件組件
import MsgConfirm from "MsgConfirm.vue";
var MyComponent = Vue.extend(MsgConfirm);
// 創(chuàng)建并掛載到 #app (會替換 #app)
new MyComponent().$mount('#app')
// 同上
new MyComponent({ el: '#app' })
----------------以上是第一種寫法,以下是第二種寫法-----------------
// 或者,在文檔之外渲染并且隨后掛載
var component = new MyComponent().$mount()
document.getElementById('app').appendChild(component.$el)