如何在打字稿中访问scss变量

Kar*_*k s 6 sass typescript angular

我想生成一个对象,它将创建一个 HTML 元素。这里我需要为该对象提供颜色、背景颜色、边框。

我不想对所有这些值进行硬编码,而是想从 SCSS 中获取它们。

我尝试了这个方法并且有效。不确定这是正确的方法还是不正确的方法。所以,寻找完美的解决方案

SCSS示例代码:

:root {
    --normal-rate: #00008b;
    --special-rate : #80d4ff;
    --flat-rate : #F7941D;
}
Run Code Online (Sandbox Code Playgroud)

打字稿代码:

this.normalRate = window.getComputedStyle(document.documentElement).getPropertyValue("--normal-rate");
this.specialRate = window.getComputedStyle(document.documentElement).getPropertyValue(RateColor.SpecialRate);
this.flatRate = window.getComputedStyle(document.documentElement).getPropertyValue(RateColor.FlatRate);

this.obj= [
        { title: '', allDay: false,..., backgroundColor: this.specialRate, borderColor: this.specialRate },
        { title: '', allDay: false,..., backgroundColor: this.normalRate, borderColor: this.normalRate },
        { title: '', allDay: false,..., backgroundColor: this.flatRate, borderColor: this.flatRate }
      ];
Run Code Online (Sandbox Code Playgroud)