在Angular 2 Material中,我可以在运行时切换主题时在Components SCSS文件中使用SCSS Variable,如$ primary-dark吗?

时间:2017-10-04 18:18:53

标签: angular sass material-design angular-material2

具体地,

我希望能够实现..在任何组件SCSS文件中有多个主题和引用$primary-dark

将实施Theming your Angular Material app

中的多个主题

正在寻找使用指令的解决方案。

我被告知不可能。

1 个答案:

答案 0 :(得分:1)

*由于评论而更新*

如果您想要即时交换它们,您还必须定义site.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 default theme (same as the example above).
$candy-app-primary: mat-palette($mat-indigo);
$candy-app-accent:  mat-palette($mat-pink, A200, A100, A400);
$candy-app-theme:   mat-light-theme($candy-app-primary, $candy-app-accent);

// Include the default theme styles.
@include angular-material-theme($candy-app-theme);

// Define an alternate dark theme.
$dark-primary: mat-palette($mat-blue-grey);
$dark-accent:  mat-palette($mat-amber, A200, A100, A400);
$dark-warn:    mat-palette($mat-deep-orange);
$dark-theme:   mat-dark-theme($dark-primary, $dark-accent, $dark-warn);

// Include the alternative theme styles inside of a block with a CSS class. You can make this
// CSS class whatever you want. In this example, any component inside of an element with
// `.unicorn-dark-theme` will be affected by this alternate dark theme instead of the default theme.
.unicorn-dark-theme {
  @include angular-material-theme($dark-theme);
}

您必须将.unicorn-dark-theme类添加到您想要成为主题的任何组件中。

我还没有真正完成动态主题,但我希望您能够在根级别放置ng-class并根据需要进行切换。

在@tugates最初发布的主题链接中有一个标题为多个主题的部分

此外,如果您点击该链接右上角的小油漆桶图标,您将看到4种不同的主题选项。

如果您想玩一些演示代码,请在本地克隆material2 repo,安装依赖项,然后运行npm run demo-app。玩一些实例的好方法。

*原始答案*

我们有一个site.scss文件,用于声明我们所有的变量,例如

$grey-color: mat-color(mat-palette($mat-grey));

然后我们将site.scss文件导入到我们需要使用的任何其他.scss文件中。

@import '../../../styles/site.scss';

.set-for-deletion {
  text-decoration: line-through;
  color: $grey-color;
}

我们正在为我们的构建使用angular-cli,因此我们将样式文件包含在.angular-cli.json文件中,就像您在链接中所说的那样。

  

如果您使用的是Angular CLI,则内置支持将Sass编译为css;你只需要为"样式"添加一个新条目。在angular-cli.json中指向主题文件的列表(例如,unicorn-app-theme.scss)。