使用Angular 2/Meteor动态更改主题

Kiv*_*ivo 1 themes meteor angular

我有一个Meteor/Angular 2应用程序,我想在用户点击两个可用选项之一时更改主题:

<select onChange="changeTheme()">
<option value="blue"> Blue</option>
<option value="gray">Gray</option>
</select>
Run Code Online (Sandbox Code Playgroud)

我的应用程序默认加载蓝色主题,它在main.scss下定义:

@import '../node_modules/@angular/material/core/theming/all-theme';
$app-primary: md-palette($md-light-blue, 700, 100, 700);
Run Code Online (Sandbox Code Playgroud)

我的问题是,当用户选择特定的主题时,我可以在何处以及在何处更改主题,例如格雷?这是我应该在Meteor.startup()函数中做的事情吗?或在组件内部?

谢谢

max*_*992 5

首先,你应该看看卡拉的优秀演示:https: //www.youtube.com/watch?v = 0q9FOeEELPY

然后,回答你的问题,这是你应该怎么做的:

your-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);
}
Run Code Online (Sandbox Code Playgroud)

your-html-file.html:

<div [class.dark-theme]="isDarkTheme">
<button (click)="toggleTheme()">Toggle theme</button>
Run Code Online (Sandbox Code Playgroud)

your-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;
  }
}
Run Code Online (Sandbox Code Playgroud)

当然,如果您需要的不仅仅是切换,因为您有> 2个主题,只需将主题的类传递给[class]属性而不是做[class.dark-theme]="someBoolean".