antd样式按需引入和主题颜色修改

按照官方文档一步步实现 结果还是遇到坑了,记录下

下面直接上步骤

按需引入css样式

安装craco

yarn add @craco/craco

修改 package.json 里的 scripts 属性

/* package.json */
"scripts": {
-   "start": "react-scripts start",
-   "build": "react-scripts build",
-   "test": "react-scripts test",
+   "start": "craco start",
+   "build": "craco build",
+   "test": "craco test",
}

然后在项目根目录创建一个 craco.config.js 用于修改默认配置。

module.exports = {
    // antd 按需引入样式
    babel:{
        plugins: [
            [
                "import", // 要做按需引入,所以是import
                {
                    "libraryName": "antd", // 表示要做antd的按需引入
                    "libraryDirectory": "es",// antd用es的模块化规范
                    "style": true //设置为true即是less
                }
            ]
        ]
    },
};

这样就实现了组件样式按需引入,也就是引入antd组件时自动引入了该组件样式。不需要全局引入 'antd/dist/antd.css’了;

自定义主题颜色

安装 craco-less

yarn add craco-less

修改 craco.config.js 文件


// antd样式是用less写的  在编译成css
const CracoLessPlugin = require('craco-less');
module.exports = {
    // 修改主题颜色
    plugins: [{
        plugin: CracoLessPlugin,
        options: {
            lessLoaderOptions: {
                lessOptions: {   
                    // modifyVars 表示要修改的变量      
                    modifyVars: { 
                        '@primary-color': 'orange'// @primary-color  antd定义的主题颜色变量
                    },
                    javascriptEnabled: true, // 表示允许用js修改antd底层的less文件
                },
            },
        },
    },],
};

这样就OK了。下面将样式按需引入与主题颜色修改的代码合并下
craco.config.js 文件


// antd样式是用less写的  在编译成css
const CracoLessPlugin = require('craco-less');
module.exports = {
    // antd 按需引入样式
    babel:{
        plugins: [
            [
                "import", // 要做按需引入,所以是import
                {
                    "libraryName": "antd", // 表示要做antd的按需引入
                    "libraryDirectory": "es",// antd用es的模块化规范
                    "style": true //设置为true即是less
                }
            ]
        ]
    },

    // 修改主题颜色
    plugins: [{
        plugin: CracoLessPlugin,
        options: {
            lessLoaderOptions: {
                lessOptions: {   
                    // modifyVars 表示要修改的变量      
                    modifyVars: { 
                        '@primary-color': 'orange'// @primary-color  antd定义的主题颜色变量
                    },
                    javascriptEnabled: true, // 表示允许用js修改antd底层的less文件
                },
            },
        },
    },],
};

实现上述步骤时,遇到了一些报错问题
第一个报错:
之前还正常,结果改为样式按需引入就报错了

查了下这个问题原因大概是最新版的antd按需引入方式改成craco了,旧版本antd不能识别craco,所以需要重新安装antd就可以了

yarn add antd

另一个报错:

这个报错重新安装下craco

yarn add @craco/craco
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现 antd-vue 的按引入,首先要在项目中安装 babel-plugin-import 插件。您可以使用以下命令进行安装: ``` npm install babel-plugin-import --save-dev ``` 安装完成后,在项目的 .babelrc 文件中添加以下配置: ``` { "plugins": [ ["import", { "libraryName": "ant-design-vue", "libraryDirectory": "es", "style": "css" }] ] } ``` 接下来,在 main.js 文件中引入要使用的组件。您可以使用 import 语句将所的组件引入,并将其注册为全局组件。例如,如果您想引入 Button 组件,可以这样做: ``` import Vue from 'vue'; import { Button } from 'ant-design-vue'; import App from './App.vue'; Vue.component(Button.name, Button); new Vue({ render: h => h(App), }).$mount('#app'); ``` 最后,在要使用组件的地方,您可以直接在模板中使用该组件。例如,在 App.vue 文件的模板中使用 Button 组件可以这样做: ``` <template> <div id="app"> <img alt="Vue logo" src="./assets/logo.png"> <a-button type="primary">Button</a-button> <HelloWorld msg="Welcome to Your Vue.js App"/> </div> </template> ``` 这样就可以按引入并使用 antd-vue 的组件了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [vue-cli4.0下antd(ant-design-vue)按引入(附带完整过程)](https://blog.csdn.net/weixin_43641850/article/details/108296677)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值