Vue Custom Element - Web Components' Custom Elements for Vue.js

You can check out Vue-custom-element demos at https://karol-f.github.io/vue-custom-element/undefined
npm install vue-custom-element --save
import vueCustomElement from 'vue-custom-element'
Vue.use(vueCustomElement);
If you are using Vue globally, just include vue-custom-element.js and it will automatically install the Vue.customElement method.
<script src="path/to/vue-custom-element.js"></script>
For cross-browser compatibility (IE9+) use Custom Elements polyfill.
<script src="https://cdnjs.cloudflare.com/ajax/libs/document-register-element/1.4.1/document-register-element.js"></script>
or
import 'document-register-element/build/document-register-element';
Vue-custom-element is a tiny wrapper around Vue components. It provides a seamless way to use Vue components in HTML, plain JavaScript, Vue, React, Angular etc., without manually initialising Vue. It’s using power of Web Components’ Custom Elements.
Vue-custom-element?
It might be confusing for users to understand the different use cases of Vue components vs Custom Elements .
Why might you need Vue-custom-element? Simple, for your Vue components user’s convenience. All they will need to do is include your JavaScript file and then they can:
<my-component></my-component>) at any time of the document lifecycle. You can use your elements in e.g. SPA application just by including HTML tag - no Vue initialization or JavaScript usage is needed. Custom Elements will auto initialize when mounted into document. You can include them in e.g. Vue, Angular or React projects and browser will take care of detecting it and initializationtag-name and Vue component object are needed for Vue.customElement() usageCheck out the demo site to see it in action.
For additional examples and detailed description check the demos page.
<widget-vue prop1="1" prop2="string" prop3="true"></widget-vue>
Vue.customElement('widget-vue', {
props: [
'prop1',
'prop2',
'prop3'
],
data: {
message: 'Hello Vue!'
},
template: '<p>{{ message }}, {{ prop1 }}, {{prop2}}, {{prop3}}</p>'
});
document.querySelector('widget-vue').prop2 // get prop value
document.querySelector('widget-vue').prop2 = 'another string' // set prop value
You can also change <widget-vue> HTML attributes and changes will be instantly reflected.
Firefox |
Chrome |
Safari |
Opera |
iOS |
Android |
|---|---|---|---|---|---|
| 59+ (–flag) | 54+ | 10.1+ | 42+ | 10.3+ | 55+ |
IE / Edge |
Firefox |
Chrome |
Safari |
Opera |
iOS |
Android |
|---|---|---|---|---|---|---|
| IE9+, Edge | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |
Additional, optional, third parameter to Vue.customElement() is options object. You can pass following methods.
‘This’ in callbacks points to Custom Element’s DOM Node.
{
// 'constructorCallback' can be triggered multiple times when e.g. vue-router is used
constructorCallback() {
console.info('constructorCallback', this);
},
// element is mounted/inserted into document
connectedCallback() {
console.info('connectedCallback', this);
},
// element is removed from document
disconnectedCallback() {
console.warn('disconnectedCallback', this);
},
// one of element's attributes (Vue instance props) is changed
attributeChangedCallback(name, oldValue, value) {
console.info('attributeChangedCallback', name, oldValue, value);
},
// Root component's definition is passed to this hook just before Vue instance creation - so you can modify it
beforeCreateVueInstance(RootComponentDefinition) {
console.info('beforeCreateVueInstance', RootComponentDefinition);
return RootComponentDefinition;
},
// Vue instance is created
vueInstanceCreatedCallback() {
console.info('vueInstanceCreatedCallback');
},
// in case of using vue-custom-element with modals, we destroy it after defined timeout. Use "null" value if you want to manually "$destroy" it.
destroyTimeout: 3000,
// only needed when using lazy-loading - 'props' are not accessible on Custom Element registration so we have to provide them
props: [],
// you can set shadow root for element. Only works if native implementation is available.
shadow: false,
// you can set CSS that will be available in Shadow DOM.
shadowCss: ''
}
Example options usage:
import MyElement from './MyElement.vue';
Vue.customElement('my-element', MyElement, {
shadow: true,
shadowCss: `
.card {
background-color: blue;
}`
});
Callbacks are executed before the lifecycle hooks from Vue component passed to Vue-custom-element. It’s a better idea to just use Vue component lifecycle hooks (e.g. created, mounted, beforeDestroy).

Inside HTML tags of the defined custom element, Vue-custom-element will create:
Custom Element HTML tag will expose API to interact with underlying Vue component - you can change HTML attributes or props, using JavaScript.
For advanced access, when exposed API is not enough, defined custom element can expose Vue instance via getVueInstance() method.
console.info(document.querySelector('widget-vue').getVueInstance())
my-element is valid, but myelement is notnpm install
npm run dev:demo
npm run build
This command will compile vue-custom-element.js and docs files.
Please take a note that npm run build will use config.build.assetsPublicPath, which is set to Github Pages path in config/index.js.
We use cookies
We use cookies to analyze traffic and improve your experience. You can accept or reject analytics cookies.