Error: The ‘decorators’ plugin requires a ‘decoratorsBeforeExport’ option, whose value must be a boolean. If you are migrating from Babylon/Babel 6 or want to use the old decorators proposal, you should use the ‘decorators-legacy’ plugin instead of ‘decorators’
背景:react 项目 + babel 7 + 使用到装饰器
错误

使用以上装饰器时,需安装相应的插件和配置某些文件,否则可能会出现以下错误

解决办法
步骤如下:
1、安装以下插件
npm install -D @babel/plugin-proposal-decorators
2、配置.babelrc 文件或package.json文件的 babel 字段值(两者二选一,不要都配置,否者会报找到两个配置文件的错)
(1)配置 .babelrc 文件
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
]
(2)配置 package.json 文件的 babel 字段值
babel: {
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
]
}
本文解决了一个在React项目中使用Babel7时遇到的装饰器插件错误。详细介绍了如何安装并配置正确的插件来避免错误,包括使用‘decorators-legacy’插件替代‘decorators’插件的方法。
1万+





