简体   繁体   English

角度:将CSS从父组件注入子组件

[英]Angular: Inject CSS from parent to child component

Currently I'm trying to create one Angular Element 目前,我正在尝试创建一个角度元素

To make the Element/Component reusable elsewhere, we need to somehow let parent/host component decide style for the Angular Element i'm working on 为了使元素/组件在其他地方可重用,我们需要以某种方式让父/宿主组件决定我正在处理的Angular元素的样式

I already read some StackOverflow material which lead me to believe all the ways are deprecated at the moment 我已经阅读了一些StackOverflow资料 ,这些资料使我相信目前不赞成使用所有方法

I think this is what we can do at the moment 我认为这是我们目前可以做的

<host-element>
      <my-element [getStyle] = "getStyleFnFromHost"> </my-element>
</host-element>

I don't feel like this is a good approach, however. 我觉得这不是一个好方法。

So what do you think I should do at the moment? 那么,您认为我目前该怎么办?

Edit: I would like to clarify that the difference from my question to the question that I refer to is that: I would like to find a good way inject CSS/Style from the parent element to the child element. 编辑:我想澄清一下,从我的问题到我提到的问题的区别是:我想找到一种从父元素向子元素注入CSS / Style的好方法。 That does not necessarily involves cascading CSS from parent to Child elements 这不一定涉及 CSS从父级元素级联为子级元素

As described in the answer you referenced, the ::ng-deep piercing combinator is still the only way to do what you're asking. 如您所引用的答案中所述, ::ng-deep穿孔组合器仍然是您要执行的操作的唯一方法。 Yes, it will very likely be deprecated in the future, but only if and when an appropriate alternative is available (which is not yet the case). 是的,将来很可能不推荐使用此方法,但是只有在适当的替代方法可用时(尚不可行)。

From the Angular docs (highlights by me): Angular文档 (我重点介绍):

The shadow-piercing descendant combinator is deprecated and support is being removed from major browsers and tools. 不推荐使用穿刺阴影的后代组合器,并且从主要的浏览器和工具中删除了对它的支持。 As such we plan to drop support in Angular (for all 3 of /deep/, >>> and ::ng-deep). 因此,我们计划放弃对Angular的支持(针对/ deep /,>>>和:: ng-deep的全部3个)。 Until then ::ng-deep should be preferred for a broader compatibility with the tools. 在此之前, 应首选 :: ng-deep 获得与工具的广泛兼容性。

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

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