React搭建项目遇到的问题记录

本文记录了在React项目中遇到的三个问题及其解决方案:如何修改项目启动的默认端口,如何解决暴露配置文件时的报错,以及如何正确配置按需引入antd的路径。在端口设置上,可以通过修改package.json的start脚本设置自定义端口;对于配置文件暴露,通过特定命令可以解决报错;在按需加载antd时,需要在webpack配置中引入bable-plugin-import并调整babel-loader选项。
摘要由CSDN通过智能技术生成

1. React修改项目启动的默认端口

默认端口3000,当已经有一个300时,在使用npm start不会像vue一样自动加一,而且会报错警告,需要手动设置端口

在package.json文件找到scripts 中的"start"修改为"set PORT=9999 && node scripts/start.js",

 

2.暴露配置文件及“Remove untracked files, stash or commit any changes, and try again.”报错

在终端运行以下命令就可把config(webpack配置在此)script文件夹暴露出来

npm run eject

但我在运行时却出现了以下错误

$ npm run eject

> admin-demo@0.1.0 eject C:\Users\Administrator\Desktop\h5\练习\react-demo\admin                                                                                                                                                                                               -demo
> react-scripts eject

NOTE: Create React App 2 supports TypeScript, Sass, CSS Modules and more without                                                                                                                                                                                                ejecting: https://reactjs.org/blog/2018/10/01/create-react-app-v2.html

? Are you sure you want to eject? This action is permanent. (y/N) y
? Are you sure you want to eject? This action is permanent. Yes
This git repository has untracked files or uncommitted changes:

.gitignore
admin-demo/

Remove untracked files, stash or commit any changes, and try again.
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! admin-demo@0.1.0 eject: `react-scripts eject`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the admin-demo@0.1.0 eject script.
npm ERR! This is probably not a problem with npm. There is likely additional log                                                                                                                                                                                               ging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Administrator\AppData\Roaming\npm-cache\_logs\2019-03-18T0                                                                                                                                                                                               3_13_45_877Z-debug.log

先把上传一下在运行一遍就可以解决了

 

3.按需引入antd的路径错误配置

由于上面把react的配置文件暴露出来了,所以需要手动去webpack里面配置,官方文档也没具体描述,感觉挺坑的这一块。

首先引入首先按需加载插件bable-plugin-import

cnpm install bable-plugin-import --save-dev 
或者
yarn add babel-plugin-import --save-dev 

然后去webpack.config.js找到babel-loader配置,修改他的options,替换为下面代码

options: {
      plugins:[['import',{
             "libraryName":"antd",
              "style":true
      }]],
              // This is a feature of `babel-loader` for webpack (not Babel itself).
              // It enables caching results in ./node_modules/.cache/babel-loader/
              // directory for faster rebuilds.
      cacheDirectory: true,
},

最后在页面文件按需引入组件就ok了。还有css别忘了引入

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值