简体   繁体   English

Angular 9 / Material - 在角度组件样式中重用材质主题变量

[英]Angular 9 / Material - Reuse of material theme variables in angular components style

I have standard angular 9 application with material theming.我有带有材料主题的标准 angular 9 应用程序。 We experienced the problem of increased bundles sizes and found out that our reuse of scss styles causes this increase.我们遇到了包大小增加的问题,并发现我们对 scss 样式的重用导致了这种增加。 The issue was that imported scss was compiled in the component as stated in this blog .问题是导入的 scss 是在此博客中所述的组件中编译的。

Initial we wanted to reused the material theming variables as follows in our angular components最初,我们想在我们的角度组件中重用材质主题变量,如下所示

@import './theme.scss';

test{ background-color: mat-color($app-primary) }

To tackle this issue we followed the blogs .advice to move such code as either global style or using sass mixins.为了解决这个问题,我们按照博客的.advice 将这些代码移动为全局样式或使用 sass mixins。 We implemented the sass-mixins as followed and reused it in our component styles:我们按如下方式实现了 sass-mixins 并在我们的组件样式中重用了它:

@mixin primary-background-color {
  background-color: mat-color($app-primary);
}
@import 'mixins.scss'
  .test {
    @include primary-background-color;
  }

Nevertheless, following this advice the bundle sizes decreased a little bit by using sass-mixins, but not as much as we would move the entire code to gobal style.尽管如此,按照这个建议,通过使用 sass-mixins,bundle 的大小会减少一点,但不会像我们将整个代码移动到全局样式那样多。

This leads so my question: Is there a recommended usage of reuse material theme colours in components styles?这导致了我的问题:在组件样式中是否推荐使用重用材料主题颜色? I cannot believe that the "solution" for my problem is moving the component styles to global style!?我不敢相信我的问题的“解决方案”是将组件样式移动到全局样式!?

Looking forward to helpfull anwsers!期待有帮助的答案! Cheers!干杯!

I got the same problem but already fixed.我遇到了同样的问题,但已经解决了。

I think the best solution is this one Link我认为最好的解决方案是这个链接

What I also add in my theme, a background variable to be possible reuse in different components like the image below..我还在我的主题中添加了一个背景变量,可以在不同的组件中重复使用,如下图所示..

Theme主题

You can do this in a different way:你可以用不同的方式来做到这一点:

There are something called 'partials'.有一种叫做“部分”的东西。 You can create partials for various things and include/import them in your component.scss file and use them您可以为各种事物创建部分并将它们包含/导入到您的 component.scss 文件中并使用它们

Example:例子:

At your root level, create a folder called styles.在您的根级别,创建一个名为样式的文件夹。 Inside styles, you can create multiple partials, one for variables, one for mixins, one for fonts etc.,在样式中,您可以创建多个部分,一个用于变量,一个用于混合,一个用于字体等,

Partials start with an underscore for their file name such as _variables.scss, _mixins.scss, etc. The underscore will prevent the angular compiler from compiling everytime.部分文件名以下划线开头,例如 _variables.scss、_mixins.scss 等。下划线将阻止 angular 编译器每次编译。

In '_variables.scss' file, you can create multiple global variables for colors, such as 

$primary-color: #fff;
$secondary-color: #333; etc

In '_mixins.scss' you can create mixins such as 
@mixin mixin-name() {
  mixins functionality
}


In your component.scss file, simply import them and use it. 

component.scss

@import '../styles/mixins';
@import '../styles/variables';

.component-class-name {
 background-color: $primary-color;
 color: $secondary-color;
}

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

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