🔸custom element

browserweb componentsimplement ⟩ custom elements

// define new custom element
customElements.define(
  'web-component',                    // ⭐️ HTML tag name, must have "-"❗️ 
  class extends HTMLElement { ... }   // ⭐️ must extend `HTMLElement`❗️ 
);

Valid Custom Element Name

  • 開頭:[a-z] ⭐️

  • 接著:[0-9], [a-z], "-", ".", "_" (optional)

  • 中間一定要有:"-" ⭐️

  • 接著:[0-9], [a-z], "-", ".", "_" (optional)

<vstack-> 或 <i-vstack> ❓(這兩個都是合法的名字)

Register New Tag Name

We can create our tags by using the CustomElementRegistry object (window.customElements).

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?