• 技术文章 >web前端 >前端问答

    react antd没有样式怎么办

    藏色散人藏色散人2022-12-28 11:19:59原创107

    react antd没有样式的解决办法:1、在项目最外层组件引入antd样式包,方式如“@import '~antd/dist/antd.css';”;2、在webpack配置中引入“ ['import', { libraryName: 'antd', style: 'css' }],”。

    本教程操作环境:Windows10系统、react18.0.0版、Dell G3电脑。

    react antd没有样式怎么办?

    react项目引入antd组件没有样式的问题

    明显是antd的样式文件没有引入进来,有两种方式解决:

    1.在项目最外层组件引入antd样式包

    如在App.css中最上面加

    @import '~antd/dist/antd.css';

    2.webpack配置引入

    module.exports = {
        entry: {
            index: path.join(srcPath, 'index.js'),
        },
        module: {
            rules: [
                // babel-loader
                {
                    test: /\.(js|jsx)$/,
                    loader: ['babel-loader?cacheDirectory'],// 开启缓存
                    include: srcPath,
                    // exclude: /node_modules/
                    loader: require.resolve('babel-loader'),
                    options: {
                        plugins: [
                            // 引入样式为 css
                            // style为true 则默认引入less
                            ['import', { libraryName: 'antd', style: 'css' }],
                        ]
                    }
                }
            ]
        },
        plugins: [
            new HtmlWebpackPlugin({
                template: path.join(srcPath, 'index.html'),
                filename: 'index.html'
            })
        ]
    }

    推荐学习:《react视频教程

    以上就是react antd没有样式怎么办的详细内容,更多请关注php中文网其它相关文章!

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:React
    上一篇:react怎么实现点击时改变样式 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • webpack4 react报错怎么办• 怎么使用docker部署react项目• react classnames用法是什么• react怎么实现通讯录• react 怎么动态修改style
    1/1

    PHP中文网