简体   繁体   English

如何在不重复混合的情况下将自定义材质主题包含到组件中:垫芯和角度材质主题

[英]How to include custom material themes into components without duplicating mixins: mat-core and angular-material-theme

I found a very helpful explanation about how to apply material theme color schemes/palettes for other non-material-components here . 我发现如何应用材料主题配色方案/调色板等非物质成分非常有帮助的解释在这里

Before I read this, I thought of something similar but couldn't imagine how to consider the recommendation from Theming your Angular Material app , if I don't want to have only global themes: 在我读这篇文章之前,我曾想过类似的东西,但是如果我不想只包含全局主题,那么我将无法想象如何考虑主题化Angular Material应用程序中的建议。

Your custom theme file should not be imported into other SCSS files. 您的自定义主题文件不应导入其他SCSS文件。 This will duplicate styles in your CSS output. 这将在CSS输出中复制样式。 If you want to consume your theme definition object (eg, $candy-app-theme) in other SCSS files, then the definition of the theme object should be broken into its own file, separate from the inclusion of the mat-core and angular-material-theme mixins. 如果要在其他SCSS文件中使用主题定义对象(例如$ candy-app-theme),则应将主题对象的定义分解为自己的文件,并与mat-core和angular分开-材料主题混合。

I wonder if this recommendation means that only global style sheets/ themes should be used? 我想知道此建议是否意味着仅应使用全局样式表/主题? Otherwise I cannot imagine how to import the scss-theme into a component's scss-file without violating the above recommendation. 否则,我无法想象如何在不违反上述建议的情况下将scss-theme导入组件的scss-file中。

I am new to Sass and maybe missing something here. 我是Sass的新手,可能在这里错过了一些东西。

I came across the same problem, some of the discussion can be found here , and an insighful blog dealing with the issue here . 我碰到了同样的问题,有些讨论可以发现这里和insighful博客处理这个问题在这里

Point is that - as the theming guide correctly states - you should never import mixins @include mat-core() and @include angular-material-theme($your-theme) more than once in your entire project. 关键是-正如主题指南正确指出的那样-在整个项目中,切勿@include mat-core()导入mixins @include mat-core()@include angular-material-theme($your-theme) But when you're working with SASS in your components, you often do want to refer to your theme variables and mat-color palette. 但是,当您在组件中使用SASS时,通常确实希望引用您的主题变量和颜色调色板。 It's tempting to import your entire theme into the component SASS, accidentally duplicating the material mixins. 将整个主题导入到SASS组件中是很诱人的,意外地复制了材质的mixins。

The solution I ended up with I think is the one the theming guide describes 我认为最终的解决方案是主题指南所描述的解决方案

the definition of the theme object should be broken into its own file, separate from the inclusion of the mat-core and angular-material-theme mixins 主题对象的定义应分为自己的文件,与包括mat-core和angular-material-theme混合包分开

but since it was initially unclear to me how to do it, here's a step by step for anyone stuck with the same: 但是由于起初我不清楚该怎么做,因此对于那些坚持使用它的人来说,这是一步一步的工作:

  1. create a assets/styles/partials folder containing SASS partials 创建一个包含SASS局部的assets/styles/partials文件夹

My folder looks like this: 我的文件夹如下所示:

assets/styles/partials/
  _palette.scss     // custom material palettes
  _scaffolding.scss // general mixins
  _theme.scss       // <-- our theme definition
  _variables.scss   // global variables like fonts and colors

The _theme partial contains the following: _theme部分包含以下内容:

// assets/styles/partials/_theme.scss
@import '~@angular/material/theming';
@import 'variables';
@import 'scaffolding';
@import 'palette';

$my-primary: mat-palette($mat-primary);
$my-accent: mat-palette($mat-accent);
$my-warn: mat-palette($mat-warn);

$my-theme: mat-light-theme($my-primary, $my-accent);

That's it. 而已。 Do not include the material mixins mat-core and angular-material-theme in this file. 包括材料混入mat-coreangular-material-theme在此文件中。

  1. create a global theme file assets/styles/my-theme.scss . 创建一个全局主题文件assets/styles/my-theme.scss It contains just three lines: 它仅包含三行:
// assets/styles/my-theme.scss
@import 'partials/theme';                    // our actual theme definition
@include mat-core();                         // the required mat-core mixin
@include angular-material-theme($my-theme);  // the declaration of our custom material theme

By doing this we have separated our theme partial, including all our custome palette, scaffolding and variables, from the file that includes mat-core and our custom material theme. 通过这样做,我们从包含垫芯和自定义材质主题的文件中分离了部分主题,包括我们的所有自定义调色板,脚手架和变量。

  1. In Angular.json, declare your my-theme file as global under styles 在Angular.json中,将my-theme文件声明为styles下的global
"styles": [ "src/assets/styles/my-theme.scss" ]

With this, our app uses our custom material theme throughout, but because the theme definition (in the theme partial) is separate from the inclusion of material mixins (in my-theme ), we can safely include our theme partial into any component without duplicating any css. 这样,我们的应用将始终使用自定义材质主题,但是由于主题定义 (在局部theme )与包含材质混合(在my-theme )是分开的,因此我们可以安全地将局部theme包含在任何组件中,而无需重复任何CSS。

Optionally, you can simplify the import of our theme partial by adding the partials path to the stylePreprocessorOptions in Angular.json: (可选)您可以通过向Angular.json中的stylePreprocessorOptions添加Partials路径来简化主题Partial的导入:

"build": {
  (...)
  "options": {
    (...)
    "stylePreprocessorOptions": {
      "includePaths": [
        "src/assets/styles/partials"
      ]
    }
  }
}

Simply @import 'theme' in any component scss file and we have access to all our variables and the material theming functions such as mat-color :) 只需在任何组件scss文件中使用@import'theme @import 'theme' ,我们就可以访问所有变量和素材主题功能,例如mat-color :)

You should have this right now : 您现在应该拥有此功能:

@import '~@angular/material/theming';
@include mat-core();

$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
$candy-app-warn:    mat-palette($mat-red);

$candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);

@include angular-material-theme($candy-app-theme);

But you should aim for that : 但您应该为此目标:

// First file variables.scss
@import '~@angular/material/theming';
@include mat-core();

$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
$candy-app-warn:    mat-palette($mat-red);
// Second file
@import 'src/variables';

$candy-app-theme: mat-light-theme($candy-app-primary, $candy-app-accent, $candy-app-warn);
@include angular-material-theme($candy-app-theme);

That's how I understood it. 这就是我的理解。

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

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