我正在做一个lit-element项目,我遇到了一个问题,reset.css不能应用于用shadow-root包装的web组件
我尝试过这种方法,得到了以下错误。
Refused to apply style from 'http://localhost:8080/style/static/reset.css' because its MIME type ('text/html') is not a supported stylesheet MIME type, and strict MIME checking is enabled.我尝试的代码是这样的:
<script>
var css = new CSSStyleSheet()
css.replace('@import url("./style/static/reset.css")')
document.adoptedStyleSheets = [css]
</script>它被放入一个html文件中。
如何避免此错误,并将reset.css应用于web组件?
发布于 2019-08-03 02:07:16
将替换导入应用于shadowroot而不是文档是否有帮助?
const node = document.createElement('div')
const shadow = node.attachShadow({ mode: 'open' })
shadow.adoptedStyleSheets = [sheet]https://wicg.github.io/construct-stylesheets/#using-constructed-stylesheets
编辑--为清晰起见添加:
上面的内容解决了将一个可能包含@import语句的样式表应用到您的原始问题所引用的框架,即阴影根节点(元素),然而,因为您的代码试图将实例化的表应用于文档,所以对我来说,这个问题变得有点模糊。
您指出的错误似乎表示代码试图应用在另一个文档中创建的样式表:
如果您尝试采用在不同文档中构造的CSSStyleSheet,则将抛出"NotAllowedError“DOMException。
https://github.com/WICG/construct-stylesheets/blob/gh-pages/explainer.md
https://stackoverflow.com/questions/57325809
复制相似问题