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

3,662 阅读1分钟

1、新建的React工程中使用“@register、@connect”之类的“@”装饰器时候,会报错“Support for the experimental syntax 'decorators-legacy' isn't currently enabled”。 这是由于没有装支持装饰器的组件导致的。 解决方法如下: 2、 运行npm run eject 暴露配置文件 打开终端,cd到工程根目录下面, 执行 npm run eject 命令。 新建的工程执行该命令会报错。因此需要先执行 1 git add . 2 git commit -m "init" 3 重新执行 npm run eject 命令 这时候可以看到 npm run eject 命令执行正常。

3、待命令执行完毕,需要正确安装装饰器,执行以下命令 npm i @babel/core @babel/plugin-proposal-class-properties @babel/plugin-proposal-decorators @babel/preset-env

4、找到工程中的package.json,将package.json中的babel依赖修改为 "babel": { "presets": [ "react-app" ], "plugins": [ [ "@babel/plugin-proposal-decorators", { "legacy": true } ] ] }

5、找到工程中的tsconfig.json 或 jsconfig.json文件,添加以下配置: "experimentalDecorators": true,

6、重新启动工程运行,此时可以解决这个报错。 如果本文对你有帮助,顺手点个赞呗!~😁