高阶组件装饰器:Support for the experimental syntax 'decorators-legacy' isn't currently enabled

  • 问题描述:
    基于类的组件使用高阶组件装饰器报错,SyntaxError: D:\workspace\test1\src\components\ReduxTest.js: Support for the experimental syntax 'decorators-legacy'
    isn't currently enabled (16:1):


  • 解决办法:
    (1)配置了按需加载:
    安装react-app-rewired取代react-scripts,可以扩展webpack的配置
    npm install [email protected] babel-plugin-import --save
    (2)配置按需加载
  • 根目录下创建config-overrides.js

内容如下:

const { injectBabelPlugin } = require("react-app-rewired");
module.exports = function override(config, env) {
  config = injectBabelPlugin(
    // 在默认配置基础上注入
    // 插件名,插件配置
    ["import", { libraryName: "antd", libraryDirectory: "es", style: "css" }],
    config
  );

  config = injectBabelPlugin(
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    config
  );

  return config;
};

然后将package.json中scripts下的react-scripts改为eact-app-rewired,如下所示:

"scripts": {
  "start": "react-app-rewired start",
  "build": "react-app-rewired build",
  "test": "react-app-rewired test",
  "eject": "react-app-rewired eject"
},

(3)安装高阶组件装饰器
npm install --save-dev babel-plugin-transform-decorators-legacy
注:我解决问题的步骤是这样的,有多余步骤未分析,先记录下,方便后边修改。

你可能感兴趣的:(高阶组件装饰器:Support for the experimental syntax 'decorators-legacy' isn't currently enabled)