简体   繁体   English

使用 css 变量在 scss 中生成 class 名称

[英]use css variable to generate class names in scss

is it possible to generate class names by css variables?是否可以通过 css 个变量生成 class 个名称?

I am using scss in angular and I want to generate my class names base on我在 angular 中使用 scss,我想生成我的 class 名称基于

@input color

property of component组件属性

so I store the input in a css variable named --color-variable to use it in my scss file.所以我将输入存储在名为 --color-variable 的 css 变量中,以便在我的 scss 文件中使用它。 I want to do something like this:我想做这样的事情:

.ps-button--color-#{var(--color-variable)}{}

this is the compiler error:这是编译器错误:

在此处输入图像描述

any suggestions would be helpfull.任何建议都会有所帮助。

is it possible to generate class names by css variables?是否可以通过 css 个变量生成 class 个名称?

Short answer:简短回答:

No.不。

Longer answer:更长的答案:

CSS custom properties, also commonly called CSS variables, are known at runtime, while SASS variables are known at compile time. CSS 个自定义属性,通常也称为 CSS 个变量,在运行时已知,而 SASS 个变量在编译时已知。 Since browsers can't understand SASS, it first needs to be compiled down to CSS and by that time, the SASS compiler won't know what the value of the CSS variable will be, so it throws a compile error.由于浏览器无法理解 SASS,因此首先需要将其编译为 CSS,到那时,SASS 编译器将不知道 CSS 变量的值是多少,因此会抛出编译错误。

Possible solution可能的解决方案

Dynamically generate CSS based on your input and add it to the DOM.根据您的输入动态生成 CSS 并将其添加到 DOM。

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

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