【问题标题】:How to generate the same color styles not multiple times in angular?如何在角度不多次生成相同的颜色样式?
【发布时间】:2020-10-23 23:24:40
【问题描述】:

在 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        

按照指南,我可以将此错误减少到至少四个到达。此错误列出了我的 node_modules 中的 custom_theme 文件和 @angular/material。 我真的很想知道我做错了什么导致这个错误。在我的 custom_theme 的第 242 行,我正在生成我的深色主题 .custom-dark-theme { @include angular-material-color($app-theme-dark); }

【问题讨论】:

  • 你找到解决方案了吗?
  • 其实是的。我会在星期一发布。
  • 请不要忘记..我真的需要它..谢谢!
  • 解决方案在哪里?
  • @MojioMS 可以分享给我们吗?

标签: angular sass angular-material


【解决方案1】:

一般要做的是避免你的组件 sccs @import-ing 一个文件本身 @includes 材料核心 mixins 之一。因此,您可以在部分中定义主题和一些可共享的主题常量/混合,然后在导入主题定义的全局样式中创建样式。更明确地说:

  • 创建一个类似_my-theme.scss 的文件,前导下划线使它成为一个sass 部分。
  • 在此文件中,仅通过调用材料的material-light-theme函数定义主题。请注意,这只是一个函数,它只是创建一个大的 Sass 贴图,材质库的其余部分使用它来实际生成样式。它本身并不像 mixins 那样“生成”样式。
  • 在全局/根样式文件中,导入'my-theme',然后导入@include color/styles mixin。
  • 在您的组件文件中,仅从我的自定义主题部分导入,而不是从根文件导入

但是,在 Sass 和 Angular 12 的更新版本中,更好的方法是使用 @import 而不是使用 @use 语法,它可以更好地处理这些部分文件中的“共享”公共代码。简单示例/入门代码

_my-theme.scss

@use "~@angular/material" as mat;

$my-theme-dark: mat.define-dark-theme(
  // stuff here
);

styles.scss


@use "~@angular/material" as mat;
@use './my-theme' as my-theme;

@include mat.core()

// general style defaults

.app-dark {
  @include mat.all-component-colors(my-theme.$my-theme-dark);
}

some-component.scss


@use "~@angular/material" as mat;
@use 'my-theme' as my-theme;

// do anything with mat and your definitions from my-theme

【讨论】:

  • 谢谢 - 这对我帮助很大。我正在使用 Angular 13(和 Angular Material),这里最重要的一点是:按照这里的建议保持文件分离!创建一个单独的“无辜”主题 SCSS,如果需要,您可以轻松地将其导入自定义组件,并在您的 styles.scss(最上面的样式文件)中创建实际主题(一个或多个 @include mat.all-component-themes(...)) )。这样这些方法就不会被多次调用,不会产生多个相同的颜色样式。
  • 是的——基本上!您基本上想避免组件 scss @import-ing 一个文件本身 @include 的材料所有组件主题等等。乐于助人
猜你喜欢
  • 2011-06-18
  • 2012-06-16
  • 2017-03-29
  • 1970-01-01
  • 2018-11-30
  • 2018-10-06
  • 2020-10-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多