简体   繁体   English

Ionic 4 中的 Shadow DOM/Web 组件样式

[英]Shadow DOM/Web component styling in Ionic 4

I'm trying to understand how and where ionic 4 is injecting the styling for the web components into the dom.我试图了解 ionic 4 如何以及在何处将 web 组件的样式注入到 dom 中。 I'm not interested in changing it via the CSS variables but I want to be able to inspect the component and see where the styling is coming from.我对通过 CSS 变量更改它不感兴趣,但我希望能够检查组件并查看样式的来源。 For example on https://ionicframework.com/docs/demos/api/alert/index.html?ionic:mode=ios例如在https://ionicframework.com/docs/demos/api/alert/index.html?ionic:mode=ios

If you inspect the button:如果您检查按钮:

离子风格的按钮

I can see the stlying being applied but I can't see where all the styling is coming from, it's not in the css bundle, style tags on the page or directly on the element or applied to the shadow-root.我可以看到正在应用的 stlying,但我看不到所有样式的来源,它不在 css 包中,不在页面上的样式标签或直接在元素上或应用于阴影根。 Normally the host styling is part of the component eg通常主机样式是组件的一部分,例如

样式化的 Web 组件

So where is the:host styling being defined and how/where is it being injected into the DOM?那么在哪里定义:host 样式以及它是如何/在哪里注入到 DOM 中的?

So as far as I know all the styles you apply to your component are usually set innerHTML.据我所知,您应用于组件的所有 styles 通常都设置为 innerHTML。 You already found the style tag which is the first tag after the component.您已经找到样式标签,它是组件之后的第一个标签。 This is super full with stylings as always thats why you usually see just a snippet.这与往常一样充满了样式,这就是为什么您通常只看到一个片段的原因。 That depends on the browser but to see all the stylings you may have to do a double click into the stylings between the style tags.这取决于浏览器,但要查看所有样式,您可能必须双击样式标签之间的样式。

在此处输入图像描述

Usually when I want to read something like this I copy it into my editor and take a look there:通常当我想阅读这样的内容时,我会将其复制到我的编辑器中并在那里查看:

在此处输入图像描述

As you can see all your stylings are defined there.如您所见,您的所有样式都在那里定义。 Stenciljs also load some general stylings to make sure everything looks proper. Stenciljs 还加载了一些通用样式以确保一切看起来都正确。 These are instantiated into your head section:这些被实例化到你的 head 部分: 在此处输入图像描述

So I think Ionic 4 is using: https://developers.google.com/web/updates/2019/02/constructable-stylesheets所以我认为 Ionic 4 正在使用: https://developers.google.com/web/updates/2019/02/constructable-stylesheets

Which is why the styling isn't visible directly in the DOM.这就是为什么样式在 DOM 中不直接可见的原因。 Credit to Fraser for working this out.感谢弗雷泽解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM