You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TailwindCSS自定义颜色无法在JavaScript中访问的问题求助

TailwindCSS自定义颜色无法在JavaScript中访问的问题求助

我们团队最近碰到了一个Tailwind CSS自定义颜色的棘手问题,想请大家帮忙排查下问题所在:

我们已经在根目录的tailwind.config.js里配置了自定义颜色,项目结构如下:

public
src
package.json
postcss.config.js
tailwind.config.js

这些自定义颜色在HTML标签里使用完全正常,比如bg-prod-100能正确渲染样式,但在Chart.js中,我们尝试通过tailwindConfig().theme.colors.prod[100]来引用这些颜色时,发现自定义的prod系列颜色根本不在tailwindConfig().theme.colors对象里,只有Tailwind的默认颜色能被正常访问到。

另外,如果我们尝试直接从src目录外导入tailwind.config.js文件,还会触发这个错误:

Module not found: Error: You attempted to import ../../tailwind.config which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.

以下是我们的相关代码:

Chart.js中的使用代码

let dataObj = {
  dates: [],
  labels: [],
  datasets: [
    {
      label: "Idle Minutes",
      data: [],
      fill: true,
      backgroundColor: tailwindConfig().theme.colors.prod[100],
      borderColor: tailwindConfig().theme.colors.prod[500],
      borderWidth: 2,
    }
  ]
}

完整的tailwind.config.js文件

const plugin = require("tailwindcss/plugin");
const colors = require("tailwindcss/colors");

module.exports = {
  content: [
    "./src/**/*.{js,jsx,ts,tsx}",
    "./public/index.html",
  ],
  theme: {
    colors: {
      ...colors,
      tempBlueLight: "#06A8FF",
      prod: {
        100: "#E8FCF6",
        200: "#A4F4D9",
      }
    },
    extend: {
      outline: {
        blue: "2px solid rgba(0, 112, 244, 0.5)"
      },
      fontFamily: {
        inter: ["Inter", "sans-serif"]
      }
    }
  },
  variants: [
    "responsive",
    "group-hover",
    "visited",
    "disabled"
  ],
  plugins: [
    require("@tailwindcss/forms"),
    require("flowbite/plugin"),
    plugin(function ({ addComponents, theme }) {
      const screens = theme("screens", {});
      addComponents([
      ]);
    })
  ]
};

我们检查过配置文件的路径和语法,看起来都没有问题,但就是没法在JS代码里拿到自定义颜色。有没有大佬能指点一下我们哪里做错了?

备注:内容来源于stack exchange,提问作者Mister AD

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.20 08:44:32