在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 問答/HTML/ react 引入ant的less文件后報錯,不知道如何才能正確引用

react 引入ant的less文件后報錯,不知道如何才能正確引用

根據(jù)ant官方的使用方法,我在react里面使用了ant

import { Table, Button } from 'antd';

這樣已經(jīng)能正常使用里面提供的組件了,

但后來我想修改Table組件里面的一些樣式,在還沒修改前,先按照官方的方法引入ant里面的less文件

import 'antd/dist/antd.less';

然后

npm run build

就報錯,如果把less改成css就不會報錯

后面使用ant官方說的按需引入方案

import DatePicker from 'antd/lib/date-picker';  // 加載 JS
import 'antd/lib/date-picker/style/css';        // 加載 CSS
// import 'antd/lib/date-picker/style';         // 加載 LESS

也是引入css就沒錯,引入less就報錯
最后改為用使用 babel-plugin-import做按需引入,

// .babelrc or babel-loader option
{
  "plugins": [
    ["import", { "libraryName": "antd", "libraryDirectory": "es", "style": "css" }] // `style: true` 會加載 less 文件
  ]
}

也是一樣
npm run build的報錯信息如下:

Failed to compile.

./node_modules/antd/es/input/style/index.less
Module build failed:


@import "./search-input";
^
Can't resolve './search-input' in 'F:\webproject\old\user\marketmgr\thirdmanager\my-app\node_modules\antd\es\input\style'
      in F:\webproject\old\user\marketmgr\thirdmanager\my-app\node_modules\antd\es\input\style\index.less (line 33, column 0)


npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! my-app@0.1.0 build: `node scripts/build.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the my-app@0.1.0 build script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\keiouks\AppData\Roaming\npm-cache\_logs\2018-02-28T01_24_26_181Z-debug.log

不管使用哪種方法,只要引入ant里面的less,就報這個錯

我已經(jīng)安裝了less和less-loader,下面是webpack的配置

          {
            test:/\.less$/,
            loaders:['style-loader','css-loader','less-loader']
          },
          // "file" loader makes sure those assets get served by WebpackDevServer.
          // When you `import` an asset, you get its (virtual) filename.
          // In production, they would get copied to the `build` folder.
          // This loader doesn't use a "test" so it will catch all modules
          // that fall through the other loaders.
          {
            // Exclude `js` files to keep "css" loader working as it injects
            // its runtime that would otherwise processed through "file" loader.
            // Also exclude `html` and `json` extensions so they get processed
            // by webpacks internal loaders.
            exclude: [/\.(js|jsx|mjs)$/, /\.html$/, /\.json$/, /\.scss$/, /\.less$/],
            loader: require.resolve('file-loader'),
            options: {
              name: 'static/media/[name].[hash:8].[ext]',
            },
          },

希望指點

回答
編輯回答
影魅

我也遇到這個問題,用的是react-create-app腳手架,然后用npm run eject命令將所有內(nèi)建的配置暴露出來,然后再安裝antd,就發(fā)生這個問題。查閱文檔:

你也可以使用 create-react-app 提供的 yarn run eject 命令將所有內(nèi)建的配置暴露出來。不過這種配置方式需要你自行探索,不在本文討論范圍內(nèi)。

所以如果你用的是react-create-app腳手架,安裝好后不用eject命令,再按照antd的文檔說明做一遍就可以了

2017年2月19日 11:09
編輯回答
神經(jīng)質(zhì)

既然使用了import plugin,就沒必要再引入樣式了。

// .babelrc or babel-loader option
{
  "plugins": [
    ["import", { "libraryName": "antd", "style": "true" }]
  ]
}

以下直接使用antd組件就可以了,不要再引入樣式了。

import { Table, Button } from 'antd'; 
2017年7月15日 16:53