使用 Angular 2 / Meteor 动态更改主题
Change theme dynamicly with Angular 2 / Meteor
我有一个 Meteor/Angular 2 应用程序,我想在用户单击两个可用选项之一时更改主题:
<select onChange="changeTheme()">
<option value="blue"> Blue</option>
<option value="gray">Gray</option>
</select>
我的应用程序默认加载蓝色主题,它在 main.scss 下定义:
@import '../node_modules/@angular/material/core/theming/all-theme';
$app-primary: md-palette($md-light-blue, 700, 100, 700);
我的问题是,当用户选择特定主题时,我可以如何以及在何处更改主题,例如 Gray?那是我应该在 Meteor.startup() 函数中做的事情吗?还是在组件内部?
谢谢
首先,你应该看看Kara的精彩介绍:
https://www.youtube.com/watch?v=0q9FOeEELPY
然后,为了回答您的问题,请按以下步骤操作:
你的-scss-file.scss :
@import '~@angular/material/core/theming/all-theme';
@include md-core();
$primary: md-palette($md-deep-purple);
$accent: md-palette($md-amber);
$theme: md-light-theme($primary, $accent);
@include angular-material-theme($theme);
.dark-theme {
$dark-p: md-palette($md-deep-purple, 700);
$dark-a: md-palette($md-amber);
$dark-t: md-dark-theme($dark-p, $dark-a);
@include angular-material-theme($dark-t);
}
你的-html-file.html :
<div [class.dark-theme]="isDarkTheme">
<button (click)="toggleTheme()">Toggle theme</button>
你的-ts-file.ts :
@Component({
selector: 'your-component-selector',
templateUrl: './your-html-file.html',
styleUrls: ['your-scss-file.scss']
})
export class YourComponent implements {
// by default, if you do not want the dark theme
private isDarkTheme = false;
constructor() { }
toggleTheme() {
this.isDarkTheme = !this.isDarkTheme;
}
}
当然,如果您需要的不仅仅是一个切换,因为您有超过 2 个主题,只需将主题的 class 传递给 [class]
属性,而不是 [class.dark-theme]="someBoolean"
。
我有一个 Meteor/Angular 2 应用程序,我想在用户单击两个可用选项之一时更改主题:
<select onChange="changeTheme()">
<option value="blue"> Blue</option>
<option value="gray">Gray</option>
</select>
我的应用程序默认加载蓝色主题,它在 main.scss 下定义:
@import '../node_modules/@angular/material/core/theming/all-theme';
$app-primary: md-palette($md-light-blue, 700, 100, 700);
我的问题是,当用户选择特定主题时,我可以如何以及在何处更改主题,例如 Gray?那是我应该在 Meteor.startup() 函数中做的事情吗?还是在组件内部?
谢谢
首先,你应该看看Kara的精彩介绍: https://www.youtube.com/watch?v=0q9FOeEELPY
然后,为了回答您的问题,请按以下步骤操作:
你的-scss-file.scss :
@import '~@angular/material/core/theming/all-theme';
@include md-core();
$primary: md-palette($md-deep-purple);
$accent: md-palette($md-amber);
$theme: md-light-theme($primary, $accent);
@include angular-material-theme($theme);
.dark-theme {
$dark-p: md-palette($md-deep-purple, 700);
$dark-a: md-palette($md-amber);
$dark-t: md-dark-theme($dark-p, $dark-a);
@include angular-material-theme($dark-t);
}
你的-html-file.html :
<div [class.dark-theme]="isDarkTheme">
<button (click)="toggleTheme()">Toggle theme</button>
你的-ts-file.ts :
@Component({
selector: 'your-component-selector',
templateUrl: './your-html-file.html',
styleUrls: ['your-scss-file.scss']
})
export class YourComponent implements {
// by default, if you do not want the dark theme
private isDarkTheme = false;
constructor() { }
toggleTheme() {
this.isDarkTheme = !this.isDarkTheme;
}
}
当然,如果您需要的不仅仅是一个切换,因为您有超过 2 个主题,只需将主题的 class 传递给 [class]
属性,而不是 [class.dark-theme]="someBoolean"
。