微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在模板

如何解决在模板

我有一个自定义元素“ thing-y”,该元素使用template和两个slot

      <template id="thingy-template">
          <details>
            <summary><slot name="thing-id">THING</slot></summary>
            <p><slot name="thing-content">VALUE</slot></p>
          </details>
      </template>

thing-y元素构造函数在阴影DOM中克隆模板

  customElements.define('thing-y',class extends HTMLElement {
    constructor() {
      super();
      const template = document
        .getElementById('thingy-template')
        .content;
      this.attachShadow({mode: 'open'})
        .appendChild(template.cloneNode(true));
    }
  });

然后在页面中的任意位置使用元素

      <thing-y id="something">
        <span slot="thing-id">Something</span>
        <span slot="thing-content">42</span>
      </thing-y>

到目前为止,很好。

现在,我想在slot的两个地方使用相同的template值,所以我将template的定义更改为

  <template id="thingy-template">
    <details>
      <summary><slot name="thing-id">THING</slot></summary>
      <p><slot name="thing-id">THING</slot> 
          content is <slot name="thing-content">VALUE</slot></p>
     </details>
  </template>

但是只有前slot内容被其值替换,第二个内容保持不变。

如何实现这个简单的要求?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。