解决react项目:Support for the experimental syntax 'decorators-legacy' isn't currently enabled


react项目中有用到高阶组件装饰器的写法,然后项目就报错,信息如下:

Support for the experimental syntax 'decorators-legacy' isn't currently enabled

解决方法:

1、安装 babel-plugin-transform-decorators-legacy

yarn add babel-plugin-transform-decorators-legacy

2、修改配置文件,package.json 中加入以下代码:

"plugins": [

["@babel/plugin-proposal-decorators", { "legacy": true }]],

此时 package.json 里面还没有 babel 配置,需要执行

yarn eject

把没有展示的react-script的配置都展示在 package.json 中

如果执行完 发现控制台报错,解决方法就是把本地的修改提交了,执行

git add
git commit -m ''

然后执行 yarn eject 

3、安装 babel-plugin-transform-decorators-legacy

4、修改 package.json 中的 babel

"babel": {

"plugins": [

["@babel/plugin-proposal-decorators", { "legacy": true }]

],

"presets": [

"react-app"

]}

参考:http://metronic.net.cn/metronic/show-51455.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM