如何在其他scss文件中使用角度材质主题颜色变量

时间:2019-10-01 09:53:49

标签: angular sass angular-material angular-material2 angular8

对于学校作业,我们需要使用角形材料。我制作了一个自定义主题:weather-theme.scss

此文件中的代码如下:


    @import '~@angular/material/theming';

    @include mat-core();

    // Define the palettes for your theme using the Material Design palettes available in palette.scss
    // (imported above). For each palette, you can optionally specify a default, lighter, and darker
    // hue. Available color palettes: https://material.io/design/color/
    $weather-theme-primary: mat-palette($mat-gray, 800, 700, 600);
    $weather-theme-accent:  mat-palette($mat-amber, 800, 700);

    // The warn palette is optional (defaults to red).
    $weather-theme-warn:    mat-palette($mat-red);

    // Create the theme object (a Sass map containing all of the palettes).
    $weather-theme-theme: mat-light-theme($weather-theme-primary, $weather-theme-accent, $weather-theme-warn);

    // Include theme styles for core and each component used in your app.
    // Alternatively, you can import and @include the theme mixins for each component
    // that you are using.
    @include angular-material-theme($weather-theme-theme);

主题起作用。

然后我将主题导入到styles.scss中,代码如下:


    @import url('https://fonts.googleapis.com/css?family=Nunito&display=swap');

    @import './weather-theme.scss';
    //@import './variables.scss';

    html, body {
        font-family: 'Nunito';
    }

    html, body { height: 100%; }
    body { margin: 0;}

现在的问题是,我想以其他样式使用Theme变量,例如我想为主体赋予原色,因为代码是这样的:

    body { 
        margin: 0;
        background-color: $weather-theme-primary;
    }

但是当我导入weather-theme.scss时,变量被scss intellisense进行了核对,这是行不通的,并且出现以下错误:

./ src / scss / styles.scss(./node_modules/@angular-devkit/build-angular/src/angular-cli-files/plugins/raw-css-loader.js!../node_modules/ postcss-loader / src嵌入!./ node_modules / sass-loader / lib / loader.js ref--15-3!./ src / scss / styles.scss) 模块构建失败(来自./node_modules/sass-loader/lib/loader.js):

background-color: $weather-theme-primary;
                 ^
  (50: #fafafa, 100: #f5f5f5, 200: #eeeeee, 300: #e0e0e0, 400: #bdbdbd, 500: #9e9e9e, 600: #757575, 700: #616161, 800: #424242, 900: #212121, A100: #ffffff, A200: #eeeeee, A400: #bdbdbd, A700: #616161, contrast: (50: rgba(0, 0, 0, 0.87), 100: rgba(0, 0, 0, 0.87), 200: rgba(0, 0, 0, 0.87), 300: rgba(0, 0, 0, 0.87), 400: rgba(0, 0, 0, 0.87), 500: rgba(0, 0, 0, 0.87), 600: white, 700: white, 800: white, 900: white, A100: rgba(0, 0, 0, 0.87), A200: rgba(0, 0, 0, 0.87), A400: rgba(0, 0, 0, 0.87), A700: white), default: #424242, lighter: #616161, darker: #757575, text: #424242, default-contrast: white, lighter-contrast: white, darker-contrast: white, "50-contrast": rgba(0, 0, 0, 0.87), "100-contrast": rgba(0, 0, 0, 0.87), "200-contrast": rgba(0, 0, 0, 0.87), "300-contrast": rgba(0, 0, 0, 0.87), "400-contrast": rgba(0, 0, 0, 0.87), "500-contrast": rgba(0, 0, 0, 0.87), "600-contrast": white, "700-contrast": white, "800-contrast": white, "900-contrast": white, "A100-contrast": rgba(0, 0, 0, 0.87), "A200-contrast": rgba(0, 0, 0, 0.87), "A400-contrast": rgba(0, 0, 0, 0.87), "A700-contrast": white, "contrast-contrast": null) isn't a valid CSS value.

╷ 13│背景颜色:$ weather-theme-primary;    │^^^^^^^^^^^^^^^^^^^^^^^    ╵   stdin 13:23根样式表       在/media/sean-paul/Data/Projects/School/CSP/OpenWeatherAngularApp/src/scss/styles.scss中(第13行,第23列)

我在这里做错了什么?

注意:我使用角度8

谢谢!

2 个答案:

答案 0 :(得分:1)

您的变量是一张地图。您必须声明所需的属性。

background-color: map-get($weather-theme-primary, 100)

答案 1 :(得分:0)

您在$weather-theme-primary中创建的weather-theme.scss不包含单个值,而是整个调色板。查看创建它的mixin的名称,并在错误消息中,您可以看到正在转储的实际对象(... 50: #fafafa, 100: #f5f5f5, ...,其中50是第一个键,#fafafa是颜色,100是第二个键,#f5f5f5是颜色,以此类推...)。

您可以通过以下方式从地图中提取值:

$my-color: map-get($weather-theme-primary, 50)

然后可以在实际的scss规则中使用该颜色。