javascript - 使用 lit-html 用 Shadow DOM 封装自定义元素的 CSS
问题描述
试图使一些css
仅适用于使用 Shadow DOM 的自定义元素。对于渲染,我使用lit-html
.
有任何想法吗?或者其他一些方法来完成没有阴影的封装?
<!DOCTYPE html>
<html>
<head>
<meta charset='UTF-8'>
<script type='module' src="script.js"></script>
<title>LitComponent</title>
</head>
<body>
<lit-component></lit-component>
<p>not styled</p>
</body>
</html>
js
import { html, render } from "./node_modules/lit-html/lit-html.js";
class LitComponent extends HTMLElement {
constructor() {
super();
this.attachShadow({mode: 'open'})
}
connectedCallback() {
let style = document.createElement('style');
style.innerHTML = `
p {
font-size: 80px;
}
`
this.shadowRoot.appendChild(style);
render(this.createView(), this);
}
createView() {
return html`
<p> styled </p>
`;
}
}
customElements.define("lit-component", LitComponent);
强文本
我希望在我LitComponent
的样式中包含“样式”段落,font-size: 80px;
而不是普通标记段落。
但实际上,当像这样附加阴影时,它根本没有渲染我的组件。
解决方案
解决了
render(this.createView(), this.shadowRoot);
推荐阅读
- angular - 如何将节点模块导入 Web Worker 脚本?
- reactjs - 多个 useEffect React.useEffect 缺少依赖项
- python - 如何遍历具有相同名称形式的变量数组?
- windows - ElasticSearch 实例在 Windows [7.3.1] 的网络内不可见
- javascript - 通过 javascript 访问 svg 元素可以在线工作,但不能在本地文件中
- asp.net-core - ASP.NET Core 选项模式,名称由单个下划线分割
- php - 尽管代码的顺序如何,如何使一个 html 元素一个接一个地加载?
- python - 如何更快地遍历大量文件夹
- javascript - 函数 doGet 在离开批准脚本中不起作用
- r - 余弦函数没有给出正确的结果(它给出了正确的数字但符号相反)