Weekly Vue News — #66 — Defining and Registering Vue Web Components

Michael Hoffmann
4 min readNov 7, 2022

--

Hi 👋

I’m available for work from 9st January 2023, see tweet below.

This week I’ll work on a theme for Slidev, an amazing tool for developers to create presentations.

Have a great week ☀️

Vue Tip: Defining and Registering Vue Web Components

Web Components is an umbrella term for a set of web native APIs that allows developers to create reusable custom elements. Vue has excellent support for both consuming and creating custom elements.

The main advantage of web components is that they can be used with any framework or even without a framework.

In three simple steps, let’s look at how we can create a web component from a Vue SFC (single-file component).

1. Create the custom element

To create the custom element, we use defineCustomElement:

Image created with CodeSnap.dev

You may have noticed that our SFC uses the special file ending .ce.vue.

It inlines the SFC’s <style> tags as strings of CSS and exposes them under the component's styles option.

During production build with default tooling setup, the <style> inside the SFCs are extracted and merged into a single CSS file. But for the custom component, the <style> tags should be injected into the custom element's shadow root.

2. Register the custom element with the DOM

Next, we can register the custom element using the define method. After registration, all <my-example> tags on the page will be upgraded afterward:

Image created with CodeSnap.dev

3. Use the custom element in your HTML

Finally, we can use our custom element in HTML:

Image created with CodeSnap.dev

Check out the docs for more details on how this works.

Curated Vue Content

📕 Hybrid Rendering in Nuxt.js 3

vueschool.io

👉🏼 Nuxt 3 has a lot of rendering options. There’s SSR, SSG, SPA…

👉🏻 In this article, Daniel breaks down each option.

📕 A guide to automated testing in Vue with Cypress

blog.logrocket.com

👉🏻 In this article, Paul teaches us how to test individual Vue components with Cypress.

👉🏻 “If you’re using Vue, Cypress is one of the best tools available for testing your application.”

📹 Vue’s Road to Vite

viteconf.org

👉🏻 As Haoqun shared in his talk at ViteConf, it’s not only the speed that makes Vite attractive.

👉🏻 Slides: https://docs.google.com...

TypeScript Tip: Avoid Default Exports

Let’s first take a look at an example with default exports using export default:

Image created with CodeSnap.dev

And now the same example but without the default export:

Image created with CodeSnap.dev

Why is it better?

🤜🏻 Better refactoring support

🤜🏻 Typo protection

🤜🏻 Better tree shaking

🤜🏻 Auto import / completion

Curated Web Development Content

📕 Type-level TypeScript

type-level-typescript.com

👉🏻 An online course to take your TypeScript skills from intermediate to advanced.

👉🏻 It will give you a solid understanding of the type system’s fundamentals and guide you through its most advanced features.

📕 Let’s Talk About Web Components

bradfrost.com

👉🏻 Brad looks at how web components can help deliver design system component libraries and their compatibility with JS libraries and frameworks.

📕 Toyota Suffered a Data Breach by Accidentally Exposing A Secret Key Publicly On GitHub

blog.gitguardian.com

👉🏻 Toyota revealed they had accidentally exposed a credential allowing access to customer data in a public GitHub repo for nearly 5 years.

🛠️ DevTools-X

github.com

👉🏻 A cross-platform collection of offline-first developer utilities similar to those native to Mac/Windows.

👉🏻 Includes utilities for color, images, JSON, Regex, SQL, and even a live React playground.

🛠️ SurveyJS

surveyjs.io

👉🏻 A form builder library for creating data-driven, multi-langauge survey forms.

👉🏻 It has a ‘pro’ level and some related commercial services but is MIT licensed at the base.

🛠️ Day.js

day.js.org

👉🏻 Fast 2kB alternative to Moment.js with the same modern API.

Quote of the Week

--

--

Michael Hoffmann
Michael Hoffmann

Written by Michael Hoffmann

Senior Frontend Developer (Freelancer) from Germany focused on Vue.js

No responses yet