網站首頁 編程語言 正文
1、添加antd組件樣式不生效
在入口文件中引入import 'antd/dist/antd.css'
樣式生效,但是生成警告
WARNING in ./node_modules/antd/dist/antd.css
Failed to parse source map: 'webpack://antd/./components/time-
picker/style/index.less' URL is not supported
這樣就需要在webpack中配置,react項目默認的配置文件是不顯示的,需要運行指令“yarn eject”暴露配置文件
運行時又遇到問題2
解決完問題2后
解決1的方法是在webpack.config.dev.js和webpack.config.prod.js文件添加相關配置,然后引入antd.less
暴露出webpack配置后,在webpack.config.js 中更改配置如下
// style files regexes
const cssRegex = /\.(css|less)$/;//此行為更改行!!!!!!!!!
const cssModuleRegex = /\.module\.css$/;
const sassRegex = /\.(scss|sass)$/;
const sassModuleRegex = /\.module\.(scss|sass)$/;
const hasJsxRuntime = (() => {
if (process.env.DISABLE_NEW_JSX_TRANSFORM === 'true') {
return false;
}
try {
require.resolve('react/jsx-runtime');
return true;
} catch (e) {
return false;
}
})();
// This is the production and development configuration.
// It is focused on developer experience, fast rebuilds, and a minimal bundle.
module.exports = function (webpackEnv) {
const isEnvDevelopment = webpackEnv === 'development';
const isEnvProduction = webpackEnv === 'production';
// Variable used for enabling profiling in Production
// passed into alias object. Uses a flag if passed into the build command
const isEnvProductionProfile =
isEnvProduction && process.argv.includes('--profile');
// We will provide `paths.publicUrlOrPath` to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_URL%/xyz looks better than %PUBLIC_URL%xyz.
// Get environment variables to inject into our app.
const env = getClientEnvironment(paths.publicUrlOrPath.slice(0, -1));
const shouldUseReactRefresh = env.raw.FAST_REFRESH;
// common function to get style loaders
const getStyleLoaders = (cssOptions, preProcessor) => {
const loaders = [
isEnvDevelopment && require.resolve('style-loader'),
isEnvProduction && {
loader: MiniCssExtractPlugin.loader,
// css is located in `static/css`, use '../../' to locate index.html folder
// in production `paths.publicUrlOrPath` can be a relative path
options: paths.publicUrlOrPath.startsWith('.')
? { publicPath: '../../' }
: {},
},
{
loader: require.resolve('css-loader'),
options: cssOptions,
},
//此{}為添加行!!!!!!!!!!!!!!!!!!!
{
loader:'less-loader',
options:{
javascriptEnabled: true
}
},
運行代碼,后又遇到報錯看問題3
報錯:Less Loader has been initialized using an options object that does not match the?API?schema?
2、運行yarn eject時暴露配置文件報錯
This git repository has untracked files or uncommitted changes: ?.DS_Store
git add .
git commit -am "Save before ejecting"
然后再運行就可以了
這樣的webpack文件中就有了
3、less-loader版本過高,刪除舊版本,下載低版本即可
yarn remove less-loader
yarn add less-loader@4.0.1
?再運行,代碼成功運行
4、項目中引入icon代碼報錯
export 'Icon' (imported as 'Icon') was not found in 'antd'
Ant Design 從 v3 升級到 v4 導致
圖標升級(點擊可查看文檔),舊版 Icon 使用方式將被廢棄,你將仍然可以通過兼容包繼續使用:
import { Icon } from '@ant-design/compatible';
運行時如果沒有安裝包,運行指令 yarn add @ant-design/compatible
再重新運行代碼就可以了
補充:React中antd按需加載樣式不生效解決辦法
按照antd官網文檔,執行按需加載操作后,樣式不生效,很可能是因為在webpack.config.js文件中設置了css模塊化;
解決辦法:
在終端中執行
npm run eject
彈出config文件夾后,找到webpack.config.js文件
{
test: cssRegex,
exclude: cssModuleRegex,
use: getStyleLoaders({
importLoaders: 1,//在這行后面添加 modules:false
modules:false,
sourceMap: isEnvProduction
? shouldUseSourceMap
: isEnvDevelopment,
}),
總結
原文鏈接:https://blog.csdn.net/Sunny_lxm/article/details/123567654
相關推薦
- 2022-08-18 Kotlin函數使用示例教程_Android
- 2022-06-01 利用20行Python?代碼實現加密通信_python
- 2023-05-06 react中定義變量并使用方式_React
- 2023-01-03 Android序列化實現接口Serializable與Parcelable詳解_Android
- 2022-02-21 MyBatis There is no getter for property named ‘xxx
- 2022-03-20 C語言基礎雙指針移除元素解法_C 語言
- 2022-04-25 Pycharm下載pyinstaller報錯:You?should?consider?upgradi
- 2022-06-09 Redis超詳細講解高可用主從復制基礎與哨兵模式方案_Redis
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支