Create-React-App 中怎么使用装饰器不报错

报了一个错折腾了大半天:
 

Support for the experimental syntax 'decorators-legacy' isn't currently enab

试了网上各种办法,

什么安装

npm install @babel/plugin-proposal-decorators

再往package.json里面添加一堆代码

"babel": {
    "plugins": [
      [
        "@babel/plugin-proposal-decorators",
        {
          "legacy": true
        }
      ]
    ],
    "presets": [
      "react-app"
    ]
  },

或者修改vscode的配置:

把添加某个值为true;

或者根目录下:
 

新建jsconfig.json

{
  "compilerOptions": {
      "experimentalDecorators": true
  }
}

通通不行。

vscode重启了无数次

终于找到了一种可行的办法:
 

npm install  customize-cra react-app-rewired @babel/plugin-proposal-decorators --save

根目录新建:config-overrides.js


const { override, addDecoratorsLegacy } = require('customize-cra');
module.exports = override(
 addDecoratorsLegacy()
 );

package.json修改:
"scripts": {
 "start": "react-app-rewired start",
 "build": "react-app-rewired build",
 "test": "react-app-rewired test",
 "eject": "react-app-rewired eject"
  },

搞定!

 

你可能感兴趣的:(react)