Web组件 - 每个插槽的多个元素

发布于 2025-02-05 04:56:50 字数 437 浏览 5 评论 0原文

我正在研究新的网络组件,并遇到了有关插槽的一些问题。

我的外部容器组件内部有一个“主槽”,其中多个元素应插入(中)。但是,只能添加每个命名插槽的一个元素。

我的问题:有没有办法将多个元素添加到一个名称插槽中?如下所示:

<own-container>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
</own-container>

I'm working on new web-components and ran into some issues concerning slots.

I have an outer container-component with a "main-slot" inside, in which multiple elements should be inserted (into the same slot). However, it is only possible to add one element per named slot.

My question: is there a way to add multiple elements to one named slot? Like shown here:

<own-container>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
   <own-element slot="main"></own-element>
</own-container>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情魔剑神 2025-02-12 04:56:50

还有 comestative&lt; slot&gt;

super().attachShadow({
        mode: 'open',
        slotAssignment: 'manual' // imperative assign only
      })

您可以在一个Web组件上获得命名 lots或命令插槽,

以模拟名为 &lt; slot&lt; slot&gt;,将内容分配给同一命名&lt; slot&gt;
可能需要“ nofollow noreferrer”> mutation obserer observer api api

附加

您 每个插槽元素:

<component-with-slots>
    <H1 slot="title">Web Components are great!</H1>
    <h2 slot="title">A bit hard to master</h2>
    <b slot="title">But Great!</b>
</component-with-slots>

<script>
  customElements.define('component-with-slots', class extends HTMLElement {
    constructor() { 
        super()
          .attachShadow({mode:'open'})
          .innerHTML = `<slot name="title"></slot>`; 
    }
  });
</script>

There is also imperative <slot>

super().attachShadow({
        mode: 'open',
        slotAssignment: 'manual' // imperative assign only
      })

But! you get Named slots OR Imperative slots, on one Web Component

To mimic named <slot>, assigning content to the same named <slot>
you probably need the Mutation Observer API

addendum

You can have multiple elements per slot:

<component-with-slots>
    <H1 slot="title">Web Components are great!</H1>
    <h2 slot="title">A bit hard to master</h2>
    <b slot="title">But Great!</b>
</component-with-slots>

<script>
  customElements.define('component-with-slots', class extends HTMLElement {
    constructor() { 
        super()
          .attachShadow({mode:'open'})
          .innerHTML = `<slot name="title"></slot>`; 
    }
  });
</script>

溺ぐ爱和你が 2025-02-12 04:56:50

没有。命名插槽不可能。诀窍是要有包装器div元素来包装您的Lightdom

<own-container>
  <div slot="main">
    <own-element></own-element>
    <own-element></own-element>
    <own-element></own-element>
    <own-element></own-element>
  </div>
</own-container>

如果存在其他div导致样式问题,则可以使用新的内容显示框的类型。

div {
  display: contents;
}

一个元素的孩子看起来像是元素父母的直接子女,而忽略了元素本身。但是,请注意,它可能导致可访问性问题。

Nope. It is not possible for named slot. The trick is to have a wrapper div element to wrap your lightDOM.

<own-container>
  <div slot="main">
    <own-element></own-element>
    <own-element></own-element>
    <own-element></own-element>
    <own-element></own-element>
  </div>
</own-container>

If the presence of additional div causes styling problem, then you can use new contents type of display box.

div {
  display: contents;
}

The display: contents causes an element's children to appear as if they were direct children of the element's parent, ignoring the element itself. However, note that it can cause accessibility issues.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文