问题
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'.
解释
“decorators”插件需要一个“decoratorsBeforeExport”选项,其值必须是布尔值。如果您正在从Babylon/Babel 6 迁移过来,或者想使用旧的decorator,您应该使用“decorator -legacy”插件而不是“decorator”。
最开始我想使用rudex的装饰器connect,然后搜了一下博客
第一步:安装babel-plugin-transform-decorators-legacy插件
cnpm install babel-plugin-transform-decorators-legacy
第二步:进行配置
npm run eject
在package.json找到
"babel": {
"presets": [
"react-app"
],
}
在里面添加:
"babel": {
"presets": [
"react-app"
],
"plugins": [
"transform-decorators-legacy"
]
}
结果报错
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'.
继续寻找解决方法:
安装:
cnpm install @babel/plugin-proposal-decorators --save-dev
配置:
"babel": {
"presets": [
"react-app"
],
"plugins": [
["@babel/plugin-proposal-decorators", { "legacy": true }],
["@babel/plugin-proposal-class-properties", { "loose" : true }]
]
}
}
问题解决
如果用的是vscode有提示报错 “experimentalDecorators”
在项目根目录下添加jsconfig.json文件
{
"compilerOptions": {
"experimentalDecorators": true
}
}