🔸custom element
browser ⟩ web components ⟩ implement ⟩ custom elements
⭐ custom element name 中間一定要有:"-" ❗
例如:
<vstack->或<v-stack>✅但 "
-" 不能放前面:<-vstack>❌
define new custom elements with customElements.define().
// define new custom element
customElements.define(
'web-component', // ⭐️ HTML tag name, must have "-"❗️
class extends HTMLElement { ... } // ⭐️ must extend `HTMLElement`❗️
);custom elements render as inline elements by default.
getter/setter for attributes - make element's attributes available as properties.
Window ⟩ .customElements (CustomElementRegistry)
📘 Using custom elements ⭐️
📘 HTMLElement
Google ⟩ Custom Elements ⭐️⭐️⭐️
HTML Spec ⟩ valid custom element name
Valid Custom Element Name
Register New Tag Name
We can create our tags by using the CustomElementRegistry object (window.customElements).
custom element names must contain a hyphen❗ 📘 customElements.define()
Custom elements cannot be self-closing because HTML only allows a few elements to be self-closing. Always write a closing tag (
<custom-element></custom-element>) 📘 Google ⟩ Custom Elements
Supporting Class
To create a custom element, we need to tell the browser several details about it: how to show it, what to do when the element is added or removed to page, etc.
Last updated
Was this helpful?