【问题标题】:Angular Material2 use different theme (colors) for different builds/environmentsAngular Material2 对不同的构建/环境使用不同的主题(颜色)
【发布时间】:2018-06-11 09:46:15
【问题描述】:

我有一个 Angular6 应用程序,它使用 CLI 和 material2 以及自定义主题。现在对于另一个客户,我想使用相同的应用程序,但颜色不同。我怎样才能做到这一点?我不想维护第二个代码库,所以它必须与我认为的构建和/或环境有关?

我在styles.scss中导入的主题:

@import '~@angular/material/theming';
// Plus imports for other components in your app.

// Include the common styles for Angular Material. We include this here 
so that you only
// have to load a single css file for Angular Material in your app.
// Be sure that you only ever include this mixin once!
@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.
$app-primary: mat-palette($mat-blue, 500);
$app-accent: mat-palette($mat-blue, 900);

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

// Create the theme object (a Sass map containing all of the palettes).
$app-theme: mat-light-theme($app-primary, $app-accent, $app-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($app-theme);

【问题讨论】:

    标签: angular angular-cli angular-material2 angular6 angular-cli-v6


    【解决方案1】:

    如果您使用 angular 6,则可以使用 angular.json 中的 fileReplacements 配置部分。 使用它,您可以让文件 customer1.theme.scss 仅在为特定环境构建时替换文件 theme.scss。

    这是一个例子:

    {
      "projects": {
        "myproject": {
          "architect": {
            "build": {
              "configurations": {
                "production": {
                  "fileReplacements": [
                    {
                      "replace": "projects/customer/src/environments/environment.ts",
                      "with": "projects/customer/src/environments/environment.prod.ts"
                    },
                    {
                      "replace": "projects/customer/src/themes/theme.scss",
                      "with": "projects/customer/src/themes/theme.prod.scss"
                    }
                  ]
                }
              }
            }
          }
        }
      }
    }
    

    【讨论】:

    • 看起来很有希望!你能举个例子吗?
    • 我在anwser中放了一个例子
    • 哇,这正是我所需要的。但是 6.0.* 中的一个错误会阻止替换 html 和 scss 文件。似乎已在 6.1 中修复。*
    • 不幸的是,这不适用于scss 文件。仅支持 .ts 文件,html 很快就会支持。
    【解决方案2】:

    您可以采取的一种方法是为此使用基本的 CSS 类。在您的主题文件中,定义不同的主题:

    @import '~@angular/material/theming';
    
    @include mat-core();
    
    // Define a default theme
    $light-default-primary: mat-palette($mat-blue);
    $light-default-accent:  mat-palette($mat-blue-grey, A200, A100, A400);
    $light-default-warn:    mat-palette($mat-red);
    $light-default-theme:   mat-light-theme($light-default-primary, $light-default-accent, $light-default-warn);
    @include angular-material-theme($light-default-theme);
    
    .light-blue-theme {
    
      $light-blue-primary: mat-palette($mat-blue);
      $light-blue-accent:  mat-palette($mat-blue-grey, A200, A100, A400);
      $light-blue-warn:    mat-palette($mat-red);
      $light-blue-theme:   mat-light-theme($light-blue-primary, $light-blue-accent, $light-blue-warn);
    
      @include angular-material-theme($light-blue-theme);
    }
    
    .dark-theme {
    
      $dark-primary: mat-palette($mat-cyan);
      $dark-accent:  mat-palette($mat-blue-grey, A200, A100, A400);
      $dark-warn:    mat-palette($mat-deep-orange);
      $dark-theme:   mat-dark-theme($dark-primary, $dark-accent, $dark-warn);
    
      @include angular-material-theme($dark-theme);
    }
    

    在您的 evnironments.ts 文件中,定义客户:(我猜您需要为每个客户提供单独的环境)

    export const client = {
     clientName: 'xxx'
    }
    

    在你的组件中你可以设置当前客户:

    export class AppComponent { 
      public clientName: string = this.env.client.clientName; // imported from environments.ts
    ...
    

    然后在您的模板中,您可以将相关的类添加到您的主容器中:

    <div [class.light-blue-theme]="clientName === 'xxx'" [class.dark-theme]="clientName === 'yyy'">
      ...
    </div>
    

    【讨论】:

    • 谢谢,有个问题。我在某处使用以下行:background-color: mat-color($app-primary)。这显然行不通。有什么想法吗?
    • 不是 100% 确定,但我认为这应该仍然有效,因为它将使用当前活动主题的原色。
    • 编译错误无法识别变量$app-primary
    • 你可以尝试添加一个默认主题(见编辑)。否则,您可能需要考虑不同的方法。
    • 一个可能的解决方案是:material.angular.io/guide/theming-your-components 但是这要求您在每个组件中创建一个 mixin 文件/函数。
    猜你喜欢
    • 2017-08-29
    • 2017-06-17
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 2012-03-31
    • 2022-07-19
    • 2012-02-22
    • 1970-01-01
    相关资源
    最近更新 更多