admin管理员组文章数量:1403369
When I set breakpoint on the console.log and warn statements they are hit. However, looking at the console shows nothing.
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<link rel="stylesheet" href="./header-component.css">
<header>
<div class="nav-links" id="nav-container">
<button type="button" id="add-image">Add Image</button>
<button type="button" id="add-label">Add Label</button>
</div>
<slot name="header-content">Default Header Content</slot>
</header>
`;
customElements.whenDefined(headerComponent).then(() => {
requestAnimationFrame(() => {
const button = this.shadowRoot?.querySelector("button");
console.log(`Adding click handler ${headerComponent}`) <=======
button.addEventListener('click', () => console.log("Clicked"));
} else {
console.warn("Button not found inside shadowRoot"); <=======
}
});
});
When I set breakpoint on the console.log and warn statements they are hit. However, looking at the console shows nothing.
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.shadowRoot.innerHTML = `
<link rel="stylesheet" href="./header-component.css">
<header>
<div class="nav-links" id="nav-container">
<button type="button" id="add-image">Add Image</button>
<button type="button" id="add-label">Add Label</button>
</div>
<slot name="header-content">Default Header Content</slot>
</header>
`;
customElements.whenDefined(headerComponent).then(() => {
requestAnimationFrame(() => {
const button = this.shadowRoot?.querySelector("button");
console.log(`Adding click handler ${headerComponent}`) <=======
button.addEventListener('click', () => console.log("Clicked"));
} else {
console.warn("Button not found inside shadowRoot"); <=======
}
});
});
Share
Improve this question
asked Mar 12 at 14:16
JWPJWP
6,9753 gold badges57 silver badges77 bronze badges
5
|
1 Answer
Reset to default 1shadowDOM (or anything) can not block console output.
► A. check devtools for filtering options.
► B. does any other code overload console?
Side Note
Working with HTML is great when working with HTML.
Nodes, methods and properties are easier with createElement
<script>
customElements.define('custom-buttons', class extends HTMLElement {
constructor() {
// yes, you can use JavaScript *before* the super() call
const createElement = (tag,props) => Object.assign(document.createElement(tag),props);
super() // set and return 'this' scope
.attachShadow({ mode: 'open' }) // set and return 'this.shadowRoot'
.append(
createElement("style",{
innerText: `button{cursor:pointer;color:green}`
}),
this.addIMG = createElement('button', {
type: "button",
textContent : "add Image",
onclick : (evt) => console.log( this.addIMG.textContent )
}),
this.addLabel = createElement('button', {
type: "button",
textContent : "add Label",
onclick : (evt) => console.log( this.addLabel.textContent )
})
)// append
} // constructor
})
</script>
<custom-buttons></custom-buttons>
本文标签:
版权声明:本文标题:web component - ShadowRoot layer will not honor console.log("xyz") or warn. Nothing gets printed after it exec 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744747597a2622979.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
headerComponent
? – zer00ne Commented Mar 12 at 15:43headerComponent
defined? – zer00ne Commented Mar 13 at 17:24headerComponent
? If so, where? – zer00ne Commented 2 days ago