Moj*_*oMS 6 sass angular-material angular
在 Angular 中,我生成一个自定义主题文件、一个根样式文件,并且所有组件都有自己的自定义样式文件。但是,自从我更新到版本 10 后,我的终端就收到了垃圾邮件警告
WARNING: The same color styles are generated multiple times. Read more about how style duplication can be avoided in a dedicated guide. https://github.com/angular/components/blob/master/guides/duplicate-theming-styles.md
node_modules/@angular/material/_theming.scss 1648:7 -mat-check-duplicate-theme-styles()
node_modules/@angular/material/_theming.scss 7010:3 angular-material-theme()
node_modules/@angular/material/_theming.scss 7061:3 angular-material-color()
src/_custom_theme.scss 242:3 @import
stdin 2:9
Run Code Online (Sandbox Code Playgroud)
按照指南,我可以将这个错误减少到至少四次到达。此错误列出了我的custom_theme 文件和我的node_modules 中的@angular/material。我真的想知道我做错了什么导致这个错误。在我的 custom_theme 的第 242 行,我正在生成我的深色主题.custom-dark-theme { @include angular-material-color($app-theme-dark); }
一般要做的事情是避免您的组件 sccs @import-ing 一个文件,该文件本身@include就是材料核心混合之一。因此,您可以在部分中定义主题以及一些可共享的主题常量/混合,然后在导入主题定义的全局样式中创建样式。更明确地说:
_my-theme.scss,前导下划线使其成为 sass 部分。material-light-theme 'my-theme',然后@include颜色/样式 mixin。然而,对于 Sass 和 Angular 12 的最新版本,更好的方法是@import使用@use地处理这些部分文件中的 \xe2\x80\x9csharing\xe2\x80\x9d 公共代码的语法,而不是使用 。简单示例/起始代码
_my-theme.scss
@use "~@angular/material" as mat;\n\n$my-theme-dark: mat.define-dark-theme(\n // stuff here\n);\nRun Code Online (Sandbox Code Playgroud)\n根styles.scss
\n@use "~@angular/material" as mat;\n@use './my-theme' as my-theme;\n\n@include mat.core()\n\n// general style defaults\n\n.app-dark {\n @include mat.all-component-colors(my-theme.$my-theme-dark);\n}\n\nRun Code Online (Sandbox Code Playgroud)\nsome-component.scss
\n@use "~@angular/material" as mat;\n@use 'my-theme' as my-theme;\n\n// do anything with mat and your definitions from my-theme\nRun Code Online (Sandbox Code Playgroud)\n