whenDefined() 方法在 CustomElementRegistry interface returns a Promise that resolves when the named element is defined.

句法

Promise<> customElements.whenDefined(name);
					

参数

名称

Custom element name.

返回值

A Promise that resolves to undefined when the custom element is defined. If the custom element is already defined, the promise is resolved immediately.

异常

异常 描述
SyntaxError If the provided name is not a valid custom element name , the promise rejects with a SyntaxError .

范例

此范例使用 whenDefined() to detect when the custom elements that make up a menu are defined. The menu displays placeholder content until the actual menu content is ready to display.

<nav id="menu-container">
  <div class="menu-placeholder">Loading...</div>
  <nav-menu>
    <menu-item>Item 1</menu-item>
    <menu-item>Item 2</menu-item>
     ...
    <menu-item>Item N</menu-item>
  </nav-menu>
</nav>
					
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 => customElements.whenDefined(button.localName)
);
// Wait for all the children to be upgraded,
// then remove the placeholder.
await Promise.all(promises);
container.removeChild(placeholder);
					

规范

规范 状态 注释
HTML 实时标准
The definition of 'customElements.whenDefined()' in that specification.
实时标准 初始定义。

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
whenDefined Chrome 66
66 注意事项
注意事项 Support for 'Customized built-in elements' as well.
54 注意事项
注意事项 Support for 'Autonomous custom elements' only.
Edge 79
79 注意事项
注意事项 Support for 'Customized built-in elements' as well.
79 注意事项
注意事项 Support for 'Autonomous custom elements' only.
Firefox 63
63
不支持 59 — 63 Disabled
Disabled From version 59 until version 63 (exclusive): this feature is behind the dom.webcomponents.customelements.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 59 Disabled
Disabled Until version 59 (exclusive): this feature is behind the dom.webcomponents.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
IE 不支持 No Opera 53
53 注意事项
注意事项 Support for 'Customized built-in elements' as well.
41 注意事项
注意事项 Support for 'Autonomous custom elements' only.
Safari 10.1
10.1 注意事项
注意事项 Supports 'Autonomous custom elements' but not 'Customized built-in elements'
WebView Android 66
66 注意事项
注意事项 Support for 'Customized built-in elements' as well.
54 注意事项
注意事项 Support for 'Autonomous custom elements' only.
Chrome Android 66
66 注意事项
注意事项 Support for 'Customized built-in elements' as well.
54 注意事项
注意事项 Support for 'Autonomous custom elements' only.
Firefox Android 63
63
不支持 59 — 63 Disabled
Disabled From version 59 until version 63 (exclusive): this feature is behind the dom.webcomponents.customelements.enabled preference (needs to be set to true ). To change preferences in Firefox, visit about:config.
不支持 ? — 59 Disabled
Disabled Until version 59 (exclusive): this feature is behind the dom.webcomponents.enabled preference (needs to be set to true ) 和 preference (needs to be set to ). To change preferences in Firefox, visit about:config.
Opera Android 47
47 注意事项
注意事项 Support for 'Customized built-in elements' as well.
41 注意事项
注意事项 Support for 'Autonomous custom elements' only.
Safari iOS 10.3
10.3 注意事项
注意事项 Supports 'Autonomous custom elements' but not 'Customized built-in elements'
Samsung Internet Android 9.0
9.0 注意事项
注意事项 Support for 'Customized built-in elements' as well.
6.0 注意事项
注意事项 Support for 'Autonomous custom elements' only.

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

元数据

  • 最后修改:
  1. CustomElementRegistry
  2. 方法
    1. define()
    2. get()
    3. upgrade()
    4. whenDefined()

版权所有  © 2014-2026 乐数软件    

工业和信息化部: 粤ICP备14079481号-1