sass在angular中有多个主题

sass在angular中有多个主题,angular,sass,themes,angular4,Angular,Sass,Themes,Angular4,假设我在angular app>2中有两个主题(亮和暗),一个切换按钮已经配置为将主题类添加到body标记中 变量.scss $themes: ( light: ( backgroundColor: #fff, textColor: #408bbd, borderColor: #254fe5, ), dark: ( backgroundColor: #222, textColor: #ddd, borderColor: #2e4dd2,

假设我在angular app>2中有两个主题(亮和暗),一个切换按钮已经配置为将主题类添加到body标记中

变量.scss

$themes: (
  light: (
    backgroundColor: #fff,
    textColor: #408bbd,
    borderColor: #254fe5,
  ),
  dark: (
    backgroundColor: #222,
    textColor: #ddd,
    borderColor: #2e4dd2,
  ),
);
我尝试了我发现的不同方法,但都不奏效

我也尝试了最简单的方法

关于.component.scss

.theme-dark .page-title{
  color: white;
}

.theme-light .page-title{
  color: red;
}
:host-context(.theme-dark) .page-title{
  color: white;
}

:host-context(.theme-light) .page-title{
  color: red;
}
我知道body标签上的类
。主题暗
无法从组件样式中识别,但我找不到任何解决方案


我可以使用什么方法在这样的模块系统中获取多个主题?

您可以在范围有限的角度组件中使用
:host context
。以下是您的示例:

关于.component.scss

.theme-dark .page-title{
  color: white;
}

.theme-light .page-title{
  color: red;
}
:host-context(.theme-dark) .page-title{
  color: white;
}

:host-context(.theme-light) .page-title{
  color: red;
}
然后,您需要一个实例,例如一个可以在顶层更改CSS类以切换主题的服务


当您不需要支持IE11或其他传统浏览器时,请查看CSS自定义属性(CSS变量)以在运行时更改值。这将减少您的CSS文件

您可以在作用域角度组件中使用
:主机上下文
。以下是您的示例:

关于.component.scss

.theme-dark .page-title{
  color: white;
}

.theme-light .page-title{
  color: red;
}
:host-context(.theme-dark) .page-title{
  color: white;
}

:host-context(.theme-light) .page-title{
  color: red;
}
然后,您需要一个实例,例如一个可以在顶层更改CSS类以切换主题的服务


当您不需要支持IE11或其他传统浏览器时,请查看CSS自定义属性(CSS变量)以在运行时更改值。这将减少您的CSS文件

CSS为我们提供了一个名为var()的功能,它允许我们在运行时更改CSS元素的属性。
有关更多详细信息,请查看我的博客。CSS为我们提供了一个名为var()的功能,允许我们在运行时更改CSS元素的属性。
有关更多详细信息,如果您想在运行时更改主题,请查看我的博客

,应用根类和条件,以及我使用的方法,即使用webpack抛出一个变量并在angular中检索它,然后应用有关主题的sass文件name@BabarBilal我使用条件应用根类,但我不知道如何应用sass文件,我正在寻找一种从组件样式应用的方法,如果您想在运行时更改主题,请使用条件应用根类,我使用的方法是使用webpack抛出一个变量,并在angular中检索它,然后应用有关主题的sass文件name@BabarBilal我使用条件应用根类,但是我不知道如何应用sass文件,我正在寻找一种从组件样式应用的方法