🔰web component
// ⭐️ 1️⃣ 宣告自訂元素
class MyComponent extends HTMLElement {
// constructor
constructor() {
super();
// ⭐️ 2️⃣ 建立 shadow DOM
this.attachShadow({ mode: 'open' }); // returns a reference to this.shadowRoot
// ⭐️ 3️⃣ 建立樣板 4️⃣ 複製樣板內容到 shadow root
this.shadowRoot.innerHTML = `
<style> ... </style>
<div class="container"> ... </div>
`;
}// end of constructor()
}// end of MyComponent
// ⭐️ 5️⃣ 註冊標籤 ╭──tag name──╮ ╭─ class ─╮
window.customElements.define('my-component', MyComponent);宣告方式
使用方式
Last updated