vue.extend創(chuàng)建的組件 在實(shí)例化后調(diào)用$mount()掛載 組件內(nèi)不能共享原項(xiàng)目vue根實(shí)例掛載時(shí)注入的store涣旨、router等實(shí)例。
問(wèn)題描述:
var MyComponent = Vue.extend({
template: '<div>Hello!</div>'
})
// 創(chuàng)建并掛載到 #app (會(huì)替換 #app)
new MyComponent().$mount()
此時(shí)MyComponent組件內(nèi)部是無(wú)法使用以下根實(shí)例化時(shí)掛載注入的
new Vue({ router, store }).$mount('#app')
store、router等實(shí)例的。 并且最外層dom使用的a-locale-provider 之類(lèi)的國(guó)際化功能 MyComponent組件內(nèi)也不生效。
最終方案:
new MyComponent({parent: this}).$mount()
指定已創(chuàng)建的實(shí)例之父實(shí)例,在兩者之間建立父子關(guān)系汪疮。