Javascript WebComponents:如何获得阴影DOM中插槽的解析值?

Javascript WebComponents:如何获得阴影DOM中插槽的解析值?,javascript,html,web-component,native-web-component,Javascript,Html,Web Component,Native Web Component,我有一个web组件,我想调整其插槽的值 不幸的是,我无法从中获得解析值 我该怎么做 const template=document.createElement('template'); template.innerHTML=` `; 类MyComp扩展了HtmleElement{ 构造函数(){ 超级(); this.root=this.attachShadow({mode:'open'}); this.root.appendChild(template.content.cloneNode(

我有一个web组件,我想调整其插槽的值

不幸的是,我无法从中获得解析值

我该怎么做

const template=document.createElement('template');
template.innerHTML=`

`; 类MyComp扩展了HtmleElement{ 构造函数(){ 超级(); this.root=this.attachShadow({mode:'open'}); this.root.appendChild(template.content.cloneNode(true)); } connectedCallback(){ const slot=this.shadowRoot.querySelector('slot'); console.log('VALUE:',slot.innerText);//始终为空 } } 自定义元素。定义('my-comp',MyComp)
abc
您可以指定一个EventListener来监视插槽更改

MDN文档

customElements.define('my-element',类扩展了HtmleElement{
构造函数(){
超级();
这是阿塔奇沙多({
模式:“打开”
}).appendChild(document.getElementById(this.nodeName).content.cloneNode(true));
}
connectedCallback(){
this.listeners=[…this.shadowRoot.querySelectorAll(“SLOT”)].map(SLOT=>{
让name=“slotchange”;
设func=(evt)=>{
让节点=slot.assignedNodes();
console.log(`Slot${Slot.name}更改为${nodes[0].outerHTML}`)
}
slot.addEventListener(名称,func);
return()=>slot.removeEventListener(name,func);//返回清理函数!!!
})
}
disconnectedCallback(){
this.listeners.forEach(removeFunc=>removeFunc());
}
});

::开槽(*){
背景:黄色;
保证金:0;
}
::开槽(跨度){
颜色:红色;
}
你好,世界
多么美好的一天

明天好

您将带来什么


您可以指定一个EventListener来监视插槽更改

MDN文档

customElements.define('my-element',类扩展了HtmleElement{
构造函数(){
超级();
这是阿塔奇沙多({
模式:“打开”
}).appendChild(document.getElementById(this.nodeName).content.cloneNode(true));
}
connectedCallback(){
this.listeners=[…this.shadowRoot.querySelectorAll(“SLOT”)].map(SLOT=>{
让name=“slotchange”;
设func=(evt)=>{
让节点=slot.assignedNodes();
console.log(`Slot${Slot.name}更改为${nodes[0].outerHTML}`)
}
slot.addEventListener(名称,func);
return()=>slot.removeEventListener(name,func);//返回清理函数!!!
})
}
disconnectedCallback(){
this.listeners.forEach(removeFunc=>removeFunc());
}
});

::开槽(*){
背景:黄色;
保证金:0;
}
::开槽(跨度){
颜色:红色;
}
你好,世界
多么美好的一天

明天好

您将带来什么