【发布时间】:2022-09-16 15:59:43
【问题描述】:
我有一个应该接受任意元素来包装其内容的 Web 组件。虽然我可以在 Chrome 开发工具中看到插槽已正确分配,但 DOM 中没有任何内容。以前有人见过这个问题吗?
定义
class ExampleParent extends HTMLElement {
constructor() {
super();
const shadowRoot = this.attachShadow({ mode: 'open' });
shadowRoot.innerHTML = `
<slot name="as">
<slot name="prefix"></slot>
<slot></slot>
</slot>
`;
}
}
customElements.define('example-parent', ExampleParent);
调用
<example-parent
style="
min-width: 100px;
height: 40px;
border: 1px solid red;
display: inline-block;
"
>
<button slot="as"></button>
<div slot="prefix">Prefix slot</div>
Default
</example-parent>
实际结果
预期结果
源代码
https://stackblitz.com/edit/nested-slots-ahtfyf?file=index.html
【问题讨论】:
标签: javascript web-component lit