简体   繁体   English

动态导入样式和设置scss变量值

[英]Dynamic import style and set scss variable value

I have an admin template in which I would like user to provide option to switch between different theme color such as dark/light theme along with setting variable values, but I'm unable to conditionally import style files.我有一个管理模板,我希望用户在其中提供在不同主题颜色之间切换的选项,例如深色/浅色主题以及设置变量值,但我无法有条件地导入样式文件。

@if $light-theme {
    @import "./_metronic/_assets/sass/themes/layout/aside/light.scss";
    $primary: green;
} @else {
    @import "./_metronic/_assets/sass/themes/layout/aside/dark.scss";
    $primary: blue;
}

Getting error: Import directives may not be used within control directives or mixins.出现错误:不能在控制指令或 mixin 中使用导入指令。

Also it is a very long wished item to sass... as the error message returns: that is not possible.对于 sass 来说,这也是一个非常渴望的项目......因为错误消息返回:这是不可能的。 ( https://github.com/sass/sass/issues/279 ) https://github.com/sass/sass/issues/279

But what you can do is setting a theme variable and import your theme files.但是你可以做的是设置一个主题变量并导入你的主题文件。 Every theme file has an conditional output based on the theme variable:每个主题文件都有一个基于主题变量的条件 output:

$theme: 'light-theme';
@import 'path/dark-theme';
@import 'path/light-theme';


// EVEREY THEME FILE WITH STRUCTURE:
@if( $theme == 'this-theme' ){

   /* 
      here goes your theme variables or 
      if whanted your theme code in the file 
   */

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM