CustomElementRegistry: initialize() method
Khả dụng hạn chế
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Phương thức initialize() của giao diện CustomElementRegistry gắn registry này với một cây con DOM, đặt customElementRegistry của từng hậu duệ bao hàm chưa có registry nào, và thử nâng cấp mọi custom element được tìm thấy.
Cú pháp
initialize(root)
Tham số
root-
Một đối tượng
Node(thường làDocument,ShadowRoot, hoặcElement) mà các hậu duệ bao hàm của nó sẽ được gắn với registry này.
Giá trị trả về
Không có (undefined).
Ngoại lệ
NotSupportedErrorDOMException-
Ném ra nếu
CustomElementRegistrynày không phải là scoped (tức là không được tạo bằngnew CustomElementRegistry()) vàrootlà một nodeDocumenthoặcDocumentcủa node document củarootcócustomElementRegistrykhông phải làCustomElementRegistrynày.
Mô tả
Khi initialize() được gọi, nó sẽ duyệt qua các hậu duệ bao hàm của root theo thứ tự cây. Với mỗi phần tử (hoặc Document/ShadowRoot ở gốc) có null customElementRegistry, nó đặt registry đó thành CustomElementRegistry này. Sau đó nó thử nâng cấp từng phần tử có customElementRegistry khớp với registry này.
Một khi customElementRegistry của một node đã được đặt thành một đối tượng CustomElementRegistry, nó không thể thay đổi được. Điều này có nghĩa là initialize() chỉ có thể đặt registry trên những node vẫn còn null. Tuy nhiên, nó vẫn sẽ cố nâng cấp bất kỳ phần tử nào có customElementRegistry đã khớp với registry này, không chỉ các phần tử vừa được gán.
Node có customElementRegistry là null trong một số trường hợp, bao gồm:
- Các tài liệu được tạo bởi
DOMImplementation.createHTMLDocument(), vốn có custom element registry mặc định lànull. Các element được tạo trong những tài liệu như vậy cũng có registry lànull. - Các shadow root được tạo với
customElementRegistryđặt thànhnullthông quaElement.attachShadow(). - Các shadow root khai báo được tạo từ phần tử
<template>với thuộc tínhshadowrootcustomelementregistry, thuộc tính này yêu cầu HTML parser để lại custom element registry của shadow root lànull.
Ví dụ
>Khởi tạo một shadow root với scoped registry
Ví dụ này tạo một shadow root không có custom element registry, thêm HTML chứa một custom element, rồi gọi initialize() để gắn một scoped registry. Element <my-element> sẽ được nâng cấp khi initialize() đặt registry trên shadow root và các hậu duệ của nó.
const myRegistry = new CustomElementRegistry();
myRegistry.define(
"my-element",
class extends HTMLElement {
connectedCallback() {
this.textContent = "Hello from scoped registry!";
}
},
);
const host = document.createElement("div");
document.body.appendChild(host);
// Create the shadow DOM structure without a registry
const shadow = host.attachShadow({
mode: "open",
customElementRegistry: null,
});
shadow.innerHTML = "<my-element></my-element>";
// Later, associate the scoped registry and upgrade custom elements
myRegistry.initialize(shadow);
console.log(shadow.querySelector("my-element").textContent);
// "Hello from scoped registry!"
Thông số kỹ thuật
| Thông số kỹ thuật |
|---|
| HTML> # dom-customelementregistry-initialize> |