简体   繁体   English

Angular中组件的全局scss文件和scss文件之间的区别

[英]Difference between global scss file and scss file for component in Angular

In a recent Angular 7 project, I have a component (defined in the file file-list.component.ts ) in which there is a mat-paginator (a component from Angular material component library). 在最近的Angular 7项目中,我有一个组件(在文件file-list.component.ts定义),其中有一个mat-paginator (来自Angular材质组件库的组件)。 When I want to change the background color of the mat-paginator , I first tried to put 当我想更改mat-paginator的背景颜色时,我首先尝试放置

.mat-paginator-container {
  background-color: yellow;
}

in film-list.component.scss (the stylesheet for associated with this component), the background color of the paginator did not change. film-list.component.scss (与此组件相关联的样式表)中,分页器的背景色未更改。 When I put this in app.component.scss , it did not work either. 当我将它放在app.component.scss ,它也不起作用。 But when I put it in the src/styles.css , the background color is correctly changed. 但是,当我将其放在src/styles.css ,背景颜色已正确更改。

So my questions are: 所以我的问题是:

  • What is the difference between src/styles.scss , app.component.scss and film-list.component.scss ? src/styles.scssapp.component.scssfilm-list.component.scss什么film-list.component.scss
  • What is the scope of each of these files? 这些文件的范围是什么?
  • What is the influence of body selector used in these stylesheet files? 这些样式表文件中使用body选择器有什么影响?

src/styles.scss SRC / styles.scss

Is for global CSS that will be applied across all the application and all the components. 适用于将在所有应用程序和所有组件中应用的全局CSS。 Here you can apply styling to body without problem. 在这里,您可以毫无问题地将样式应用于body

example.component.scss example.component.scss

Is the CSS that will be scoped and applied to this specific component only. 是仅将范围限定并应用于该特定组件的CSS。 Here you won't be able to apply styling to body element. 在这里,您将无法将样式应用于body元素。

You can still pierce the scoped boundaries... 您仍然可以突破范围边界...

When using components like mat-paginator inside, let say example.component.ts for example, the CSS from mat-paginator is in fact "outside" the example.component.ts component scope because mat-paginator has its own scope. 例如,在内部使用诸如mat-paginator组件时,假设example.component.ts ,则mat-paginator的CSS实际上在example.component.ts组件范围的“外部”,因为mat-paginator有其自己的范围。 Therefore you can pierce through the shadow-dom using ::ng-deep to apply CSS. 因此,您可以使用:: ng-deep来应用CSS来穿透shadow-dom。

Working example with the code below: https://stackblitz.com/edit/angular-stackoverflow-53241725 使用以下代码的工作示例: https : //stackblitz.com/edit/angular-stackoverflow-53241725

/* not working because the class is not directly inside this component */
.mat-paginator-container {
  background: yellow;
}

/* working because `ng-deep` pierce through the shadow-dom */
::ng-deep .mat-paginator-container {
  background: red;
}

Suggested Documentation 建议的文件

Official Angular documentation about styling: https://angular.io/guide/component-styles 有关样式的Angular官方文档: https : //angular.io/guide/component-styles

Great blog that explains CSS encapsulation: https://blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700 伟大的博客,解释了CSS封装: https//blog.angular.io/the-state-of-css-in-angular-4a52d4bd2700

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

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