手动槽分配不分配内容。

huangapple go评论64阅读模式
英文:

Manual slotAssignment doesn't assign contents

问题

<!-- 开始代码片段: js 隐藏: false 控制台: true Babel: false -->

<!-- 语言: lang-js -->
class RealTodo extends HTMLElement {
    constructor() {
        super();
        this.attachShadow({ mode: "open", slotAssignment: "manual" });
    }

    connectedCallback() {
        const todoTemplate = document.getElementById("todo_template");
        const todoApp = todoTemplate.content.cloneNode(true);
        this.shadowRoot.appendChild(todoApp);

        const slot = this.shadowRoot.querySelector("slot");
        console.log(slot);

        const li = document.createElement("li");
        li.textContent = "Buy food";
        slot.assign(li);
    }
}
customElements.define("real-todo", RealTodo);

<!-- 语言: lang-html -->
<template id="todo_template">
    <h1>TODO LIST</h1>
    <ol>
        <slot></slot>
    </ol>
</template>

<real-todo></real-todo>

<!-- 结束代码片段 -->

我尝试手动分配阴影DOM内的内容。
它没有分配。没有显示错误。
我在Firefox和Chrome中进行了测试。

根据规范 slot.assign(nodes); 将分配到 slotAssignment: "manual" 阴影DOM 内。

英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

class RealTodo extends HTMLElement {
	constructor() {
		super();
		this.attachShadow({ mode: &quot;open&quot;, slotAssignment: &quot;manual&quot; });
	}

	connectedCallback() {
		const todoTemplate = document.getElementById(&quot;todo_template&quot;);
		const todoApp = todoTemplate.content.cloneNode(true);
		this.shadowRoot.appendChild(todoApp);

		const slot = this.shadowRoot.querySelector(&quot;slot&quot;);
		console.log(slot);

		const li = document.createElement(&quot;li&quot;);
		li.textContent = &quot;Buy food&quot;;
		slot.assign(li);
	}
}
customElements.define(&quot;real-todo&quot;, RealTodo);

<!-- language: lang-html -->

	&lt;template id=&quot;todo_template&quot;&gt;
    &lt;h1&gt;TODO LIST&lt;/h1&gt;
		&lt;ol&gt;
			&lt;slot&gt;&lt;/slot&gt;
		&lt;/ol&gt;
	&lt;/template&gt;
  
  &lt;real-todo&gt;&lt;/real-todo&gt;

<!-- end snippet -->

I try to assign content inside shadowDom's slot manually.
It doesn't assign. No error show.
I tested in firefox, chrome.

According to spec slot.assign(nodes); will assign inside slotAssignment: &quot;manual&quot; shadowdom

答案1

得分: 1

Your "const li = document.createElement("li");" creates a Node in memory

"SLOTElement.assign()" only accepts existing DOM Element(s) in
first level lightDOM

And you don't want to use "appendChild", because that moves a DOM Element

英文:

Your const li = document.createElement(&quot;li&quot;); creates a Node in memory

SLOTElement.assign() only accepts existing DOM Element(s) in
first level lightDOM

And you don't want to use append[Child](), because that moves a DOM Element

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

customElements.define(&quot;real-todo&quot;, class extends HTMLElement {
  constructor() {
    const button = (props) =&gt; Object.assign(document.createElement(&quot;button&quot;),props);
    super()
      .attachShadow({mode: &quot;open&quot;, slotAssignment: &quot;manual&quot; })
      .append(
        button({ innerHTML:&quot;Food&quot;   , onclick : e =&gt; this.showlist(&quot;food&quot;)   }),
        button({ innerHTML:&quot;Animals&quot;, onclick : e =&gt; this.showlist(&quot;animal&quot;) }),
        document.getElementById(this.nodeName).content.cloneNode(true)
      );
  }
  connectedCallback(){
    this.showlist();
  }
  showlist( list=this.getAttribute(&quot;list&quot;) ){
    this.shadowRoot.querySelector(&quot;span&quot;).innerHTML = list;
    const li = this.querySelectorAll(`li[list*=&quot;${list}&quot;]`);
    this.shadowRoot.querySelector(&quot;slot&quot;).assign(...li);
  }
});

<!-- language: lang-html -->

&lt;template id=&quot;REAL-TODO&quot;&gt;
    &lt;h1&gt;A &lt;span&gt;&lt;/span&gt; list&lt;/h1&gt;
    &lt;ol&gt;
        &lt;slot&gt;&lt;/slot&gt;
    &lt;/ol&gt;
&lt;/template&gt;

&lt;real-todo list=&quot;animal&quot;&gt;
  &lt;li list=&quot;animal&quot;&gt;Cat&lt;/li&gt;
  &lt;li list=&quot;animal&quot;&gt;Dog&lt;/li&gt;
  &lt;li list=&quot;food&quot;&gt;Pie&lt;/li&gt;
  &lt;li list=&quot;food&quot;&gt;Beer&lt;/li&gt;
  &lt;li list=&quot;food&quot;&gt;Pizza&lt;/li&gt;
  &lt;li list=&quot;animal,food&quot;&gt;Chicken&lt;/li&gt;
  &lt;div&gt;
    &lt;li list=&quot;animal,food&quot;&gt;Never listed!!&lt;/li&gt;
  &lt;/div&gt;
&lt;/real-todo&gt;

<!-- end snippet -->

The inspector will show which LI elements are slotted:

手动槽分配不分配内容。

huangapple
  • 本文由 发表于 2023年2月6日 19:30:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75360746.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定