亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于react+antd樣式不生效問題的解決方式

 更新時間:2022年07月14日 10:10:58   作者:Sunny_lxm  
最近本人在使用Antd開發(fā)時遇到些問題,所以下面這篇文章主要給大家介紹了關(guān)于react+antd樣式不生效問題的解決方式,文中通過圖文以及實例代碼介紹的非常詳細,需要的朋友可以參考下

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文件添加相關(guān)配置,然后引入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 使用方式將被廢棄,你將仍然可以通過兼容包繼續(xù)使用:

import { Icon } from '@ant-design/compatible';
 
運行時如果沒有安裝包,運行指令 yarn add  @ant-design/compatible
 
再重新運行代碼就可以了

補充:React中antd按需加載樣式不生效解決辦法

按照antd官網(wǎng)文檔,執(zhí)行按需加載操作后,樣式不生效,很可能是因為在webpack.config.js文件中設置了css模塊化;

解決辦法:

在終端中執(zhí)行

npm run eject

彈出config文件夾后,找到webpack.config.js文件

{
              test: cssRegex,
              exclude: cssModuleRegex,
              use: getStyleLoaders({
                importLoaders: 1,//在這行后面添加 modules:false
                modules:false,
                sourceMap: isEnvProduction
                  ? shouldUseSourceMap
                  : isEnvDevelopment,
              }),

總結(jié)

到此這篇關(guān)于react+antd樣式不生效問題的解決方式的文章就介紹到這了,更多相關(guān)react+antd樣式不生效解決內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 解決react組件渲染兩次的問題

    解決react組件渲染兩次的問題

    這篇文章主要介紹了解決react組件渲染兩次的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • React中useEffect函數(shù)的使用詳解

    React中useEffect函數(shù)的使用詳解

    useEffect是React中的一個鉤子函數(shù),用于處理副作用操作,這篇文章主要為大家介紹了React中useEffect函數(shù)的具體用法,希望對大家有所幫助
    2023-08-08
  • 一文學會使用Remix寫API接口

    一文學會使用Remix寫API接口

    這篇文章主要為大家介紹了一文學會Remix寫API接口實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • React Native時間轉(zhuǎn)換格式工具類分享

    React Native時間轉(zhuǎn)換格式工具類分享

    這篇文章主要為大家分享了React Native時間轉(zhuǎn)換格式工具類,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • 解決React報錯Property value does not exist on type HTMLElement

    解決React報錯Property value does not exist&n

    這篇文章主要為大家介紹了React報錯Property value does not exist on type HTMLElement解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12
  • react中value與defaultValue的區(qū)別及說明

    react中value與defaultValue的區(qū)別及說明

    這篇文章主要介紹了react中value與defaultValue的區(qū)別及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • React開啟代理的2種實用方式

    React開啟代理的2種實用方式

    最近有不少伙伴詢問react的代理配置,自己也去試驗了一下發(fā)現(xiàn)不少的問題,在這就將所遇到的心得分享出來,這篇文章主要給大家介紹了關(guān)于React開啟代理的2種實用方式的相關(guān)資料,需要的朋友可以參考下
    2021-07-07
  • React Native按鈕Touchable系列組件使用教程示例

    React Native按鈕Touchable系列組件使用教程示例

    這篇文章主要為大家介紹了React Native按鈕Touchable系列組件使用教程示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-11-11
  • React?如何向url中添加參數(shù)

    React?如何向url中添加參數(shù)

    這篇文章主要介紹了React?如何向url中添加參數(shù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • React高級概念之Context用法詳解

    React高級概念之Context用法詳解

    在React應用中,為了讓數(shù)據(jù)在組件間共享,常見的方式是讓它們以props的形式自頂向下傳遞,如果數(shù)據(jù)要在組件樹不同層級共享,那么這些數(shù)據(jù)必須逐層傳遞直到目的地,Context如同管道,它將數(shù)據(jù)從入口直接傳遞到出口,使用Context能避免“prop-drilling”
    2023-06-06

最新評論