【发布时间】:2021-09-28 22:03:25
【问题描述】:
我喜欢 shadow dom 样式封装的想法,但我想在每个 shadow dom 中包含基本样式(重置、排版等)。
<head>
<link rel="stylesheet" href="core.css">
...
</head>
<my-component></my-component>
<script>
customElements.define('my-component', class MyComponent extends HTMLElement {
...
connectedCallback() {
this.shadow = this.attachShadow({mode: 'open'});
const linkElem = document.createElement('link');
linkElem.setAttribute('rel', 'stylesheet');
linkElem.setAttribute('href', 'core.css');
// applying exiting "core.css" to current shadow dom
this.shadow.appendChild(linkElem);
}
});
</script>
由于core.css 被调用(链接)了两次,会影响性能吗?
【问题讨论】:
标签: javascript html css web-component shadow-dom