aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/customelementregistry/index.html
blob: 4a2279fbdf24b3a790b58cf381234312bdcab309 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
---
title: CustomElementRegistry
slug: Web/API/CustomElementRegistry
tags:
  - API
  - CustomElementRegistry
  - Interface
  - Web Components
translation_of: Web/API/CustomElementRegistry
---
<div>{{DefaultAPISidebar("Web Components")}}</div>

<p><strong><code>CustomElementRegistry</code></strong>接口提供注册自定义元素和查询已注册元素的方法。要获取它的实例,请使用 {{domxref("window.customElements")}}属性。</p>

<h2 id="方法">方法</h2>

<dl>
 <dt>{{domxref("CustomElementRegistry.define()")}}</dt>
 <dd>定义一个新的<a href="/zh-CN/docs/Web/Web_Components/Custom_Elements">自定义元素</a></dd>
 <dt>{{domxref("CustomElementRegistry.get()")}}</dt>
 <dd>返回指定自定义元素的构造函数,如果未定义自定义元素,则返回<code>undefined</code></dd>
 <dt>{{domxref("CustomElementRegistry.upgrade()")}}</dt>
 <dd>Upgrades a custom element directly, even before it is connected to its shadow root.</dd>
 <dt>{{domxref("CustomElementRegistry.whenDefined()")}}</dt>
 <dd>返回当使用给定名称定义自定义元素时将会执行的 {{jsxref("Promise", "promise")}}。(如果已经定义了这样一个自定义元素,那么立即执行返回的 promise。)</dd>
</dl>

<h2 id="示例">示例</h2>

<p>以下代码来自我们的 <a href="https://github.com/mdn/web-components-examples/tree/master/word-count-web-component">word-count-web-component</a> 示例(<a href="https://mdn.github.io/web-components-examples/word-count-web-component/">see it live also</a>)。 请注意我们如何使用 {{domxref("CustomElementRegistry.define()")}} 方法在创建其类后定义自定义元素。</p>

<pre class="brush: js">// Create a class for the element
class WordCount extends HTMLParagraphElement {
  constructor() {
    // Always call super first in constructor
    super();

    // count words in element's parent element
    var wcParent = this.parentNode;

    function countWords(node){
      var text = node.innerText || node.textContent
      return text.split(/\s+/g).length;
    }

    var count = 'Words: ' + countWords(wcParent);

    // Create a shadow root
    var shadow = this.attachShadow({mode: 'open'});

    // Create text node and add word count to it
    var text = document.createElement('span');
    text.textContent = count;

    // Append it to the shadow root
    shadow.appendChild(text);


    // Update count when element content changes
    setInterval(function() {
      var count = 'Words: ' + countWords(wcParent);
      text.textContent = count;
    }, 200)

  }
}

// Define the new element
customElements.define('word-count', WordCount, { extends: 'p' });</pre>

<h2 id="规范">规范</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">规范</th>
   <th scope="col">状态</th>
   <th scope="col">备注</th>
  </tr>
  <tr>
   <td>{{SpecName("HTML WHATWG", "custom-elements.html#customelementregistry", "CustomElementRegistry")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

<h2 id="浏览器兼容性">浏览器兼容性</h2>



<p>{{Compat("api.CustomElementRegistry")}}</p>