ShadowRoot: thuộc tính customElementRegistry
Limited availability
This feature is not Baseline because it does not work in some of the most widely-used browsers.
Thuộc tính chỉ đọc customElementRegistry của giao diện ShadowRoot trả về đối tượng CustomElementRegistry liên kết với shadow root này, hoặc null nếu chưa được đặt.
customElementRegistry của một shadow root xác định các định nghĩa custom element nào sẽ được dùng để nâng cấp các phần tử bên trong cây shadow đó. Nó có thể được đặt khi shadow root được tạo thông qua tùy chọn customElementRegistry của Element.attachShadow(), hoặc sau đó bằng CustomElementRegistry.initialize(). Một khi đã được đặt thành đối tượng CustomElementRegistry, nó không thể thay đổi.
Thuộc tính này cũng khả dụng trên các đối tượng Document thông qua cùng tên thuộc tính customElementRegistry.
Giá trị
Một đối tượng CustomElementRegistry, hoặc null.
Ví dụ
>Đặt registry có phạm vi trên một shadow root
Ví dụ này tạo một scoped registry với một định nghĩa custom element và truyền nó vào Element.attachShadow(). Thuộc tính customElementRegistry trên shadow root kết quả phản ánh scoped registry đó.
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);
const shadow = host.attachShadow({
mode: "open",
customElementRegistry: myRegistry,
});
shadow.innerHTML = "<my-element></my-element>";
console.log(shadow.customElementRegistry === myRegistry); // true
console.log(shadow.querySelector("my-element").textContent);
// "Hello from scoped registry!"
Thông số kỹ thuật
| Specification |
|---|
| DOM> # dom-documentorshadowroot-customelementregistry> |