我目前正在使用 TailwindCss 为我的下一个项目构建一个组件库,我在处理 Button 组件时遇到了一个小问题。
我传入一个类似于'primary'或 的道具'secondary',它与我在中指定的颜色相匹配,tailwind.config.js然后我想使用Template literals如下方式将其分配给按钮组件:bg-${color}-500
<button
className={`
w-40 rounded-lg p-3 m-2 font-bold transition-all duration-100 border-2 active:scale-[0.98]
bg-${color}-500 `}
onClick={onClick}
type="button"
tabIndex={0}
>
{children}
</button>
Run Code Online (Sandbox Code Playgroud)
类名在浏览器中显示得很好,它显示bg-primary-500在 DOM 中,但不显示在“应用的样式”选项卡中。
主题配置如下:
theme: {
extend: {
colors: {
primary: {
500: '#B76B3F',
},
secondary: {
500: '#344055',
},
},
},
},
Run Code Online (Sandbox Code Playgroud)
但它不应用任何样式。如果我只是手动添加bg-primary-500它工作正常。
老实说,我只是想知道这是否是因为 JIT 编译器没有选择动态类名,或者我是否做错了什么(或者这不是使用 tailWind 的方式)。
欢迎任何帮助,提前致谢!