CustomElementRegistry
Baseline
Widely available
*
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2020ë 1ì.
* Some parts of this feature may have varying levels of support.
CustomElementRegistry ì¸í°íì´ì¤ë ì¬ì©ì ì§ì ìì를 ë±ë¡íê³ , 기존ì ë±ë¡í ìì를 ê°ì ¸ì¬ ì ìë ë©ìë를 ì ê³µí©ëë¤. CustomElementRegistryì ì¸ì¤í´ì¤ë¥¼ ê°ì ¸ì¤ë ¤ë©´ window.customElements ìì±ì ì¬ì©íì¸ì.
ë©ìë
CustomElementRegistry.define()-
ìë¡ì´ ì¬ì©ì ì ì ìì를 ì ìí©ëë¤.
CustomElementRegistry.get()-
주ì´ì§ ì´ë¦ì ë¶ì¸ ì¬ì©ì ì ì ììì ìì±ì를 ë°íí©ëë¤. í´ë¹íë ì´ë¦ì ì ìë ììê° ì¡´ì¬íì§ ìì¼ë©´
undefined를 ëì ë°íí©ëë¤. CustomElementRegistry.upgrade()-
ì¬ì©ì ì ì ìì를 ì§ì ì ê·¸ë ì´ëí©ëë¤. ììê° ìì ì ìë 루í¸(shadow root)ì ì°ê²°ëì§ ììë ì ê·¸ë ì´ëí ì ììµëë¤.
CustomElementRegistry.whenDefined()-
주ì´ì§ ì´ë¦ì ì¬ì©ì ì ì ììê° ë±ë¡ëë ìê° ì´ííë ë¹
Promise를 ë°íí©ëë¤. ë§ì½ í´ë¹íë ì´ë¦ì ììê° ì´ë¯¸ ì ìë ê²½ì°, ë°ííë íë¡ë¯¸ì¤ë ì¦ì ì´íí©ëë¤.
ìì
ë¤ì ì½ëë word-count-web-component ìì ìì ê°ì ¸ì 주ìì ë²ìí ê²ì
ëë¤(ì¤í ê²°ê³¼ 미리보기). ì¬ì©ì ì§ì ìì í´ëì¤ë¥¼ ìì±í í, CustomElementRegistry.define() ë©ìëë¡ ë±ë¡íë ê³¼ì ì ì´í´ë³´ì¸ì.
// ììì í´ëì¤ ìì±
class WordCount extends HTMLParagraphElement {
constructor() {
// ìì±ììì íì ì ì¼ ë¨¼ì super í¸ì¶
super();
// ìì ë¶ëª¨ì ë¨ì´ ì ì¸ê¸°
const wcParent = this.parentNode;
function countWords(node) {
const text = node.innerText || node.textContent;
return text
.trim()
.split(/\s+/g)
.filter((a) => a.trim().length > 0).length;
}
const count = `Words: ${countWords(wcParent)}`;
// ìë ë£¨í¸ ìì±
const shadow = this.attachShadow({ mode: "open" });
// í
ì¤í¸ ë
¸ë ìì± í ë¨ì´ ìë¡ ì±ì°ê¸°
const text = document.createElement("span");
text.textContent = count;
// í
ì¤í¸ ë
¸ë를 ìë 루í¸ì ì¶ê°
shadow.appendChild(text);
// ìì ì½í
ì¸ ê° ë°ëë©´ ë¨ì´ ì ì
ë°ì´í¸
setInterval(function () {
const count = `Words: ${countWords(wcParent)}`;
text.textContent = count;
}, 200);
}
}
// ìë¡ì´ ìì ì ì
customElements.define("word-count", WordCount, { extends: "p" });
ì°¸ê³ :
CustomElementRegistryë Window.customElements ìì±ì¼ë¡ ì ê·¼í ì ììµëë¤.
ëª ì¸ì
| Specification |
|---|
| HTML > # custom-elements-api > |