create-react-app项目,mobx使用装饰器报错

November 30, 2018 访问: 1,062 次

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);

添加新评论

  1. af

    a

    Reply