How to manipulate scss variables in Angular2 components

梦想与她 提交于 2019-12-10 17:47:18

问题


Is there a way I can change scss variables declared in Angular2 component? I want to add the theme dynamically based on user selection and hence need to modify the scss variables. I read about keeping all scss variables in a separate scss file and importing the same in other scss file. But can i import the same in my component file and modify the variables. Is this feasible? TIA

Currently I am using mixin. Code goes here:

// Color themes
$themes: (
  default: #ff0000,
  banana: #f1c40f,
  cherry: #c0392b,
  blueberry: #8e44ad,
  leaf: #27ae60,
  nightsky: #2980b9
);


// Helper theme mixin
@mixin theme($name, $color) {
    .#{$name}{
        background-color: lighten($color, 30%);
    }
  .#{$name} {
    h1{
      color: $color;
    }
  }
  .#{$name} {
    nav{
      a{
      color: $color;
      }
    }
  }
}

But is there a way I can alter the variables inside $themes map from Angular component.


回答1:


This is impossible by nature of Sass: it's a build-time tool, a pre-processor. Angular only knows about CSS (though the styles and styleUrls property in Component metadata).

This means that by the time Angular has got its hands on your styles, Sass has already been compiled to CSS. You cannot change Sass variables in run-time because they do not exist anymore at that point.


You can instead use custom properties and change them via regular JavaScript.



来源:https://stackoverflow.com/questions/40480320/how-to-manipulate-scss-variables-in-angular2-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!