[英]Angular Material2 use different theme (colors) for different builds/environments
I got an Angular6 app that uses the CLI and material2 with a custom theme. 我有一个Angular6应用程序,该应用程序使用带有自定义主题的CLI和material2。 Now for another customer I want to use that same app, but with different colors.
现在,对于另一个客户,我想使用相同的应用程序,但使用不同的颜色。 How can I do this?
我怎样才能做到这一点? I don't want to maintain a second code base so it has to be something with the build and/or environment I think so?
我不想维护第二个代码库,因此必须与我认为的构建和/或环境有关?
My theme which is imported in styles.scss
: 我的主题是在
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);
One approach you could take would be to use basic CSS classes for this. 您可以采用的一种方法是为此使用基本CSS类。 In your theme file, define different themes:
在主题文件中,定义不同的主题:
@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);
}
and in your evnironments.ts
file, define the customer: (you would need separate environments for each customer I guess) 并在
evnironments.ts
文件中定义客户:(我猜每个客户都需要单独的环境)
export const client = {
clientName: 'xxx'
}
In your component you can set the current customer: 在您的组件中,您可以设置当前客户:
export class AppComponent {
public clientName: string = this.env.client.clientName; // imported from environments.ts
...
Then in your template you can add the relevant class to your main container: 然后,可以在模板中将相关类添加到主容器中:
<div [class.light-blue-theme]="clientName === 'xxx'" [class.dark-theme]="clientName === 'yyy'">
...
</div>
If you're using angular 6, you can use the fileReplacements
configuration section in angular.json
. 如果您使用的角6,你可以使用
fileReplacements
在配置部分angular.json
。 Using this, you can have a file customer1.theme.scss replacing the file theme.scss only when building for a certain environment. 使用此功能,仅当在特定环境中构建时,您才可以使用文件customer1.theme.scss替换文件theme.scss。
Here is an exemple : 这是一个例子:
{
"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"
}
]
}
}
}
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.