create-react-app项目中,在 mobx 中直接使用装饰器语法会有如下报错。
Support for the experimental syntax 'decorators-legacy' isn't currently enabled
按照Mobx文档中的方法,使用 react-app-rewired
,安装react-app-rewire-mobx
包,在config-overrides.js
中配置:
const rewireMobX = require('react-app-rewire-mobx');
/* config-overrides.js */
module.exports = function override(config, env) {
config = rewireMobX(config, env);
return config;
}
会报错:
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'.
解决方法:
随着 babel 的升级,mobx 文档里的方法已经过时,新的方法是使用 plugin-proposal-decorators
。
安装依赖:
$ yarn add @babel/plugin-proposal-decorators
在config-override.js
中增加:
config = injectBabelPlugin(["@babel/plugin-proposal-decorators", { legacy: true }], config);
a