karol-f / vue-custom-element

Vue Custom Element - Web Components' Custom Elements for Vue.js
https://karol-f.github.io/vue-custom-element/
MIT License
1.97k stars 187 forks source link

Initialize vue custom component without a custom element #179

Closed devcli closed 4 years ago

devcli commented 4 years ago

How to initialize vue-custom-element via code without a custom element on a page?

Basically, I need access to the actual vue instance when the Vue app is part of the vue-custom-element.

I can see in the docs that there is a way to get access to vue instance like this:

document.querySelector('widget-vue').getVueInstance()

This only works when I have <widget-vue> element on a page.

I am creating a third party widget where <widget-vue> element is optional. But I want to access Vue instance and run methods of the Vue app even when an element is not added to the page.

Please let me know if there is a proper way to do it.

karol-f commented 4 years ago

It's the same Vue as when You define Vue.use(VueCustomElement) - so You can easily access it there.

About vue instance - every custom element instance on the page has its own, separate, vue instance (created when it's inserted to DOM) and you have access to it like you mentioned above.

Regards

devcli commented 4 years ago

Thank you!!