aboutsummaryrefslogtreecommitdiff
path: root/files/zh-cn/web/api/customelementregistry/whendefined/index.html
blob: 8c5452386dcfda898b236256d92206ae655fc8ab (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
93
94
95
96
97
98
99
100
---
title: CustomElementRegistry.whenDefined()
slug: Web/API/CustomElementRegistry/whenDefined
translation_of: Web/API/CustomElementRegistry/whenDefined
---
<p>{{APIRef("CustomElementRegistry")}}</p>

<p><span class="seoSummary"> 当一个元素被定义时{{domxref("CustomElementRegistry")}} 中的方法<code><strong>whenDefined()</strong></code> 接口返回  {{jsxref("Promise")}}.</span></p>

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

<pre class="syntaxbox">Promise&lt;&gt; customElements.whenDefined(<em>name</em>);</pre>

<h3 id="参数">参数</h3>

<dl>
 <dt>name</dt>
 <dd>自定义元素的名称</dd>
</dl>

<h3 id="返回值">返回值</h3>

<p>当自定义元素被定义时一个{{jsxref("Promise")}} 返回{jsxref("undefined")}}. 如果自定义元素已经被定义,则resolve立即执行。</p>

<dl>
</dl>

<h3 id="例外">例外</h3>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Exception</th>
   <th scope="col">Description</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td><code>SyntaxError</code></td>
   <td>如果提供的 <strong>name</strong> 不是一个有效的 <a href="https://html.spec.whatwg.org/multipage/custom-elements.html#valid-custom-element-name">自定义元素名字</a>, promise 的 reject 回调会接收到一个 <code>SyntaxError</code>.</td>
  </tr>
 </tbody>
</table>

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

<p><code><font face="Open Sans, arial, x-locale-body, sans-serif"><span style="background-color: #ffffff;">这个例子使用</span></font>whenDefined()</code> 来检测生成菜单的自定义元素何时被定义. 这个菜单显示占位符内容一直到菜单内容已经准备好显示.</p>

<pre class="brush: html">&lt;nav id="menu-container"&gt;
  &lt;div class="menu-placeholder"&gt;Loading...&lt;/div&gt;
  &lt;nav-menu&gt;
    &lt;menu-item&gt;Item 1&lt;/menu-item&gt;
    &lt;menu-item&gt;Item 2&lt;/menu-item&gt;
     ...
    &lt;menu-item&gt;Item N&lt;/menu-item&gt;
  &lt;/nav-menu&gt;
&lt;/nav&gt;
</pre>

<pre class="brush: js">const container = document.getElementById('menu-container');
const placeholder = container.querySelector('.menu-placeholder');
// Fetch all the children of menu that are not yet defined.
const undefinedElements = container.querySelectorAll(':not(:defined)');

const promises = [...undefinedElements].map(
  button =&gt; customElements.whenDefined(button.localName)
);

// Wait for all the children to be upgraded,
// then remove the placeholder.
await Promise.all(promises);
container.removeChild(placeholder);
</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("HTML WHATWG", "custom-elements.html#dom-customelementregistry-define", "customElements.whenDefined()")}}</td>
   <td>{{Spec2("HTML WHATWG")}}</td>
   <td>Initial definition.</td>
  </tr>
 </tbody>
</table>

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

<div>
<div>


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