简体   繁体   English

Angular:覆盖嵌套组件的 scss 样式

[英]Angular: override scss styles of nested component

I created a spinner component.我创建了一个微调组件。 I wanted it to be self-contained and not rely on external css, so in the component I include the scss to style it.我希望它是独立的并且不依赖于外部 css,所以在组件中我包含了 scss 来设置它的样式。 The component template is a div with a class of sbl-circ .组件模板是一个类为sbl-circ的 div。 I can add this anywhere in my app and it works as designed.我可以在我的应用程序的任何地方添加它,它可以按设计工作。

Now, I created a second component (a button).现在,我创建了第二个组件(一个按钮)。 I want to add the spinner component to this button.我想将微调组件添加到此按钮。 It works, but the spinner color is not correct for when it's inside the button.它可以工作,但是当它位于按钮内时,微调器颜色不正确。

So, I am trying to re-color the spinner with the scss for the button component.因此,我正在尝试使用按钮组件的 scss 为微调器重新着色。 So far the only way it works is if I do到目前为止,它唯一有效的方法是如果我这样做

:host ::ng-deep {
  button.btn.btn-primary {
    .sbl-circ {
      color: white;
    }
  }
}

I know that ::ng-deep is deprecated.我知道::ng-deep已被弃用。 What's the correct way for the button component to re-color any spinner components inserted inside of it?按钮组件重新着色插入其中的任何微调器组件的正确方法是什么?

Try using :host-context .尝试使用:host-context

You should be able to use it like:您应该可以像这样使用它:

:host-context(button.btn.btn-primary) .sbl-circ {
  color: white;
}

Excuse my unfamiliarity with angular.请原谅我对 angular 不熟悉。 But if I were to attempt the resulting css to not be over-written by another style, I would try to use !important as a property value in the scss.但是如果我要尝试生成的 css 不被另一种样式覆盖,我会尝试使用!important作为 scss 中的属性值。
Example:例子:

p {
    color: red !important;
}

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

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