当前位置: 首页 > Web前端 > HTML

create-react-app2.1.8版本自定义配置

时间:2023-04-02 22:57:38 HTML

使用create-react-app新建项目后,运行eject命令可以看到各种配置。根据需要引入antdesign组件1.yarn在babel配置项中添加babel-plugin-import2.package.json文件,添加["import",{"libraryName":"antd","libraryDirectory":"es","style":"css"}]添加装饰器配置1.yarnadd@babel/plugin-proposal-decorators--在dev2.package.json文件的babel配置项中,添加["@babel/plugin-proposal-decorators",{"legacy":true}]Legacy设置为true,表示使用stage1阶段装饰器语法和行为。3.在package.json文件的eslintConfig配置项中,添加"parserOptions":{"ecmaFeatures":{"legacyDecorators":true}}如果第3步没有设置,老的装饰器写法,即下面的写法还是不能用。@decoratorexportclassFoo{}的原因在Pleaseuseexport@decclassinstead中有提到:是因为目前只支持babel-eslint@11,而babel-eslint@11目前只是测试版。要在index.js底部启用HMR,请添加以下代码if(module.hot)module.hot.accept();。但是这样简单的配置无法维护state状态,每次热更新都会重置state状态。如果想保持状态,可以使用React-Hot-Loader来实现。参考自:回溯ReactHotLoader的实现@babel/plugin-proposal-decoratorsPleaseuseexport@decclassinsteadUpdateon2019-12-24windows运行npxcreate-react-appmy-app命令,报如下错误:无法从“Filesnodejsnode_cache_npx11452”安装,因为它不包含package.json文件。原因:这是npx的问题,是名字中有空格导致的。解决方案:npminstall-gcreate-react-appcreate-react-appmy-app