ant design样式不生效官方解决方案

Posted drizzle_wen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ant design样式不生效官方解决方案相关的知识,希望对你有一定的参考价值。

上一篇的解决方案好像还是会有问题,贴出官方解决方案,

在config-overrides.js里面将原内容替换成

const { injectBabelPlugin, getLoader } = require("react-app-rewired");

const fileLoaderMatcher = function(rule) {
return rule.loader && rule.loader.indexOf(`file-loader`) != -1;
};

module.exports = function override(config, env) {
// babel-plugin-import
config = injectBabelPlugin(
[
"import",
{
libraryName: "antd",
//style: ‘css‘,
style: true // use less for customized theme
}
],
config
);

// customize theme
config.module.rules[1].oneOf.unshift({
test: /\.less$/,
use: [
require.resolve("style-loader"),
require.resolve("css-loader"),
{
loader: require.resolve("postcss-loader"),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
autoprefixer({
browsers: [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9" // React doesn‘t support IE8 anyway
],
flexbox: "no-2009"
})
]
}
},
{
loader: require.resolve("less-loader"),
options: {
// theme vars, also can use theme.js instead of this.
modifyVars: { "@brand-primary": "#1DA57A" }
}
}
]
});

// css-modules
config.module.rules[1].oneOf.unshift({
test: /\.css$/,
exclude: /node_modules|antd\.css/,
use: [
require.resolve("style-loader"),
{
loader: require.resolve("css-loader"),
options: {
modules: true,
importLoaders: 1,
localIdentName: "[local]___[hash:base64:5]"
}
},
{
loader: require.resolve("postcss-loader"),
options: {
// Necessary for external CSS imports to work
// https://github.com/facebookincubator/create-react-app/issues/2677
ident: "postcss",
plugins: () => [
require("postcss-flexbugs-fixes"),
autoprefixer({
browsers: [
">1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9" // React doesn‘t support IE8 anyway
],
flexbox: "no-2009"
})
]
}
}
]
});

// file-loader exclude
let l = getLoader(config.module.rules, fileLoaderMatcher);
l.exclude.push(/\.less$/);

return config;
};
 
默认使用的是create-react-app生成是项目,使用的是ant-design

以上是关于ant design样式不生效官方解决方案的主要内容,如果未能解决你的问题,请参考以下文章

Ant Design Table表格居中写法

ant-design样式加载失败解决办法

ant design pro 当中改变ant design 组件的样式和 数据管理

避坑+解决方案:Ant-Design-Vue 走马灯Carousel 数据绑定问题

ant design pro webapi访问提示跨域的2种解决办法

ant-design-vue table前端排序,字符串排序(升序/降序)不生效