I have an issue when creating an instance of a vue component imported from a bundled rollup library, I add a global property to the root Vue instance using Vue.prototype and it is shared across all of my components, However when i create a new instance using the Vue.extend method, the newly created component has none of the prototype properties or globals they all simply return undefined.
//Main.js
Vue.prototype.$example = 'Hello there!';
//////////////////////////////
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
//App.vue
import { MyDynamicComponent } from 'my-bundled-rollup-library';
export default {
created() {
var DynamicComponent = Vue.extend(MyDynamicComponent);
console.log(this.$example) //Returns 'Hello there!' (correct)
var instance = new DynamicComponent({
propsData: {
hello:'world',
},
})
///////////////////////////////////////////
console.log(instance.$example) //Returns undefined (does not have the property)
}
}
And here is an example of the component before it's bundled
//MyDynamicComponent.vue
const MyDynamicComponent = {
props:{
hello:{
type:String
}
},
created() {
console.log(this.$example) //undefined
}
}
export default component
export { MyDynamicComponent as MyDynamicComponent }
My first thought is that somehow the component is using a different Vue instance than the one of Vue.extend
vuein my-bundled-rollup-library isn't the same asvuein Main.js . This is very specific to you setup which wasn't shown. Steps to solve this: 1. Make surevueisn't bundled with my-bundled-rollup-library . 2. Make surevuedep constraints are as loose as possible, should be peerDependencies or*in dependencies. 3. Dedupe packages withnpm dedupe, if this doesn't help then delete and reinstall node_modules. Let me know if this helps. If this doesn't, please, provide a way to reproduce the problem.