简体   繁体   English

在 css 模块 class 名称上使用变量值

[英]using a variable value on css modules class name

I'm trying use use a variable in a class name with css modules in nextjs, but I'm having a bad time figuring out the right syntax.我正在尝试使用 class 名称中的变量和 nextjs 中的 css 模块,但我很难弄清楚正确的语法。

my variable comes from the api:我的变量来自 api:

const type = red

how i'm trying to do it:我是如何做到的:

<div className={` ${styles.background} ${styles.--type}`}></div>

The result that I expect:我期望的结果:

<div className={` ${styles.background} ${styles.--red}`></div>

is there a way to do it?有办法吗?

Not sure what you are trying to achieve.不确定您要达到的目标。

One solution could be to do:一种解决方案可能是:

const type = 'red';

<div className={`${styles.background} ${styles.type}`}></div>

However if you want to use BEM and the -- notation.但是,如果您想使用 BEM 和--表示法。 You' might have to switch to brackets notation.您可能必须切换到括号表示法。

const type = '--red';

<div className={`${styles.background} ${styles[type]}`}></div>

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

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