简体   繁体   English

Angular 材料垫扩展面板主体样式未应用

[英]Angular Material mat-expansion-panel-body style not being applied

I have the following css in my component's css file:我的组件的 css 文件中有以下 css :

.mat-expansion-panel-body {
  padding: 0;
}

And, so I would expect to see this rule (even if overridden) to show up for the following dom element:而且,所以我希望看到这条规则(即使被覆盖)出现在以下 dom 元素中:

<div class="mat-expansion-panel-body">...</div>

But, all I see being applied in dev tools is:但是,我在开发工具中看到的只是:

.mat-expansion-panel-body {
  padding: 0 24px 16px;
}

I noticed that this element does not have the _ngcontent-c19 class that the other host elements do, and so I assume this is a case of view encapsulation.我注意到该元素没有其他主机元素所具有的_ngcontent-c19 class,因此我假设这是视图封装的情况。

However, after reading around with the deprecation of::ng-deep and /deep/ and other encapsulation piercing constructs to be deprecated, what is a better solution to styling this element from within my component's css file?但是,在阅读了弃用::ng-deep 和 /deep/ 以及其他要弃用的封装穿透构造之后,在我的组件的 css 文件中设置此元素的样式的更好解决方案是什么?

I set up ::ng-deep in component css, it worked in my side.我在组件 css 中设置了::ng-deep ,它在我身边工作。

::ng-deep .mat-expansion-panel-body{
     padding: 0;
}

One possible solution if you don't want to use ::ng-deep is to set the style in your styles.css file like so.如果您不想使用 ::ng-deep,一种可能的解决方案是在您的 styles.css 文件中像这样设置样式。

.mat-expansion-panel-body {
  padding: 0 !important;
}

This will remove the padding, but be careful as it will remove it from all of the mat-expansion-panel-body elements.这将删除填充,但要小心,因为它会将它从所有 mat-expansion-panel-body 元素中删除。 You can bypass this by setting a specific class to your expansion panel and then doing something like this in styles.css您可以通过为扩展面板设置特定类,然后在styles.css 中执行类似操作来绕过此问题

.my-special-class .mat-expansion-panel-body {
  padding: 0;
}

In this case you don't even need !important.在这种情况下,您甚至不需要 !important。 Here is the example of this.这是一个例子。

https://stackblitz.com/edit/angular-a6necw https://stackblitz.com/edit/angular-a6necw

for "@angular/material": "^10.2.7"对于"@angular/material": "^10.2.7"

You add encapsulation: ViewEncapsulation.None property into your @Component decorator您将encapsulation: ViewEncapsulation.None属性添加到您的 @Component 装饰器中

@Component({
  selector: '...',
  templateUrl: '...',
  styleUrls: ['...'],
  encapsulation: ViewEncapsulation.None
})
export class examplePage{}

Then this CSS will work fine with !important然后这个 CSS 将与 !important 一起工作

.mat-expansion-panel-body {
  padding: 0 !important;
}

Note that ::ng-deep and /deep/ will not work on the new version of angular/material请注意::ng-deep/deep/不适用于新版本的 angular/material

Below how it worked for me:以下是它对我的工作方式:

::ng-deep div.mat-expansion-panel-body {
  padding: 0 !important;
}
:host ::ng-deep .mat-expansion-panel-body {
     padding: 0 !important;
 }

Works the best, now you don't have to think about this style being applies across all your components.效果最好,现在您不必考虑将这种样式应用于您的所有组件。 Using :host applies it only to the current host component使用:host仅将其应用于当前主机组件

I will aggregate all the answers and give some solutions:我将汇总所有答案并给出一些解决方案:

  1. use ng-deep like ::ng-deep.mat-expansion-panel-body in your component CSS - pretty nice solution but Angular team decided to depracte ng-deep在组件 CSS 中使用ng-deep::ng-deep.mat-expansion-panel-body - 非常好的解决方案,但 Angular 团队决定弃用ng-deep
  2. use encapsulation: ViewEncapsulation.None - personally I think is the worst solution as you will set global classes which can conflict with existing, new classes or dynamic ones (like ads on some sites)使用encapsulation: ViewEncapsulation.None - 我个人认为是最糟糕的解决方案,因为您将设置可能与现有、新类或动态类冲突的全局类(如某些网站上的广告)
  3. component-name.mat-expansion-panel-body in the global styles.(s)css - the only disadvantage is that you have to maintain the selector in case someone renames the component's selector全局styles.(s)css中的component-name.mat-expansion-panel-body - 唯一的缺点是您必须维护选择器,以防有人重命名组件的选择器
  4. move the .mat-expansion-panel-body { padding: 0 } in the global styles.(s)css file and affect all mat-expansion s components from now on.移动全局styles.(s)css文件中的.mat-expansion-panel-body { padding: 0 }并从现在开始影响所有mat-expansion的组件。

Personally I still prefer the option 1 or 3, but in my team we chose the 4th option.就我个人而言,我仍然更喜欢选项 1 或 3,但在我的团队中,我们选择了第 4 个选项。

 Please use ngdeep code here before all inbuilt classes in angular::ng-deep.mat-expansion-panel-body{ padding: 0; }

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

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