为什么我不能直接设置 angular material 步进元素的样式?

Why can't I style angular material stepper elements directly?

这是我从 official angular documentation for stepper: https://stackblitz.com/edit/angular-tth817

中分叉出来的代码示例

在我的 fork 中,我试图实现一些事情:

你能告诉我吗:

来自 angular 文档:

The styles specified in @Component metadata apply only within the template of that component.

(https://angular.io/guide/component-styles#style-scope)

换句话说,在此文件中添加样式不会影响子组件。

请注意,Angular 提供了特殊的 CSS select 或者您可以用于 select 子组件。这些在技术上已被弃用,但目前没有提及将取代它们的位置。

::ng-deep .mat-horizontal-stepper-header-container {
  border: solid 1px red; 
}

::ng-deep mat-stepper-horizontal, mat-stepper-horizontal {
  border: dashed 1px blue; 
  padding: 1em;
}

根据当前 Angular Material documentation,除了上述已弃用的方法外,您还有另外两个选择:

  • 将覆盖样式添加到您的全局样式 sheet。确定选择器的范围,使其仅影响您需要它的特定元素。
  • 在您的组件上关闭视图封装。如果您这样做,请务必适当地确定您的样式的范围,否则您最终可能会意外地以应用程序中其他地方的其他组件为目标。