Use Vue.js to create custom web components


Web Components let you define new HTML tags, referred to as custom elements. These tags can then be used in your app’s HTML code directly, like this :

 id="my-app">
  

Introduction text

In this example, will be interpreted by the browser and “replaced” by the HTML markup you have defined. This will result in :

 id="my-app">
  

Introduction text

id="share-buttons"> href="#">Facebook href="#">Twitter

It may also include custom JS logic, for instance the Facebook and Twitter links could listen to click events and share the current page when a link is clicked.

Web components are similar to Vue.js components. They have a lifecycle, properties, and can be nested. They have a different API that is less powerful but standard, defined by W3C specs.

Problem : web components are not fully supported by browsers yet. See browser support for Web Components on are-we-componentized-yet or caniuse.com

But, with a bit of JS magic you can now turn your Vue.js component into web components, enabling you to use it in any web application, even using React, Angular or .

How to turn your Vue.js component into universal web components

vue-custom-element is a library written by that allows you to use a Vue.js component as a custom element.

The HTML way

Once you register the custom element, you can insert its tag into standard HTML, SPA’s, React, Angular or Vue.js projects.

 id="my-app">
  

Introduction text

gplus="false"/>
id="share-buttons-template"> id="share-buttons"> href="#" @click.prevent="share" v-if="facebook">Facebook href="#" @click.prevent="share" v-if="twitter">Twitter href="#" @click.prevent="share" v-if="gplus">Google+

This implementation requires Vue.js core files and the vue-custom-element library to be included in your page.

Props are automatically interpreted to their native type (“false” as an attribute is interpreted as the boolean value false).

The custom element’s API is accessible like any HTML element : document.getElementsByTagName('share-buttons').

This example uses an HTML