gpt4 book ai didi

reactjs - 使用 craco 在运行时更改 antd 变量

转载 作者:行者123 更新时间:2023-12-04 03:39:12 25 4
gpt4 key购买 nike

我必须能够在运行时更改 ant 设计变量(而不是通过无主题文件)。
我用 找到了很多这样的例子定制-cra react-app-rewire-less ,但似乎没有一个适用于 craco。我必须使用 craco,因为我也在使用 尾风css 在这个项目中。
我试过的:

  • antd-theme-webpack-plugin : 我可以访问 window.less.modifyVars ,但它似乎什么都不做(调用它不会引发错误,但 antd 颜色不会改变);
  • antd-theme-switcher : 和上面很相似,window.less.modifyVars似乎没有效果;
  • antd-theme : 我不知道如何添加 AntdThemePlugin.loadercraco.config.js ,我不确定这是问题所在,但我无法让它工作。

  • 这是我的 craco.config.js 的当前状态:
    const path = require("path");
    const CracoAntDesignPlugin = require("craco-antd");

    const AntDesignThemePlugin = require("antd-theme-webpack-plugin");
    const options = {
    antDir: path.join(__dirname, "./node_modules/antd"),
    stylesDir: path.join(__dirname, "./src"),
    varFile: path.join(__dirname, "./src/styles/variables.less"),
    themeVariables: ["@primary-color"],
    indexFileName: false,
    generateOnce: false,
    };
    const ThemePlugin = new AntDesignThemePlugin(options);

    module.exports = {
    style: {
    postcss: {
    plugins: [require("tailwindcss"), require("autoprefixer")],
    },
    },
    plugins: [
    {
    plugin: CracoAntDesignPlugin,
    options: {
    customizeThemeLessPath: path.join(
    __dirname,
    "./src/styles/variables.less"
    ),
    lessLoaderOptions: {
    lessOptions: {
    javascriptEnabled: true,
    modifyVars: {
    "@primary-color": "#00375B",
    },
    },
    },
    },
    },
    { plugin: ThemePlugin },
    ],
    };
    在这一点上,我真的要尝试任何可能的解决方案,这个问题真的很耗时。

    最佳答案

    到今天为止,我已经开始工作了。第二个解决方案( antd-theme-switcher )实际上按预期工作。我的错误是我在我的主文件中添加了 antd 默认变量,但是为了使它工作,我必须在我的公共(public)文件夹中添加 color.less 文件(正如 antd-theme-switcher 中的第二步所说),所以那window.less.modifyVars有一个较少的文件可以处理。
    不过,这似乎不是最具性能的方法,我将尽快在我的项目中使用 antd,因为似乎没有最佳解决方案来使用这种特定设置在运行时更改变量。

    关于reactjs - 使用 craco 在运行时更改 antd 变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66345610/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com