带有装饰器和类属性的Babel配置不起作用

问题描述 投票:0回答:1

我有一个简单的类,需要使用Jest进行测试。我正在使用的项目的babel配置-

Babel版本-7

{
  "plugins": [
    "@babel/plugin-transform-react-jsx",
    "@babel/plugin-proposal-function-bind",
    "@babel/plugin-transform-modules-commonjs",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-transform-classes",
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ],
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    [
      "@babel/plugin-transform-runtime",
      {
        "regenerator": true
      }
    ]
  ],
  "presets": ["@babel/preset-env", "@babel/react"]
}


我遇到错误-

  SyntaxError: Missing class properties transform.
  5 | 
       6 | export default class Template {
    >  7 |   id;

javascript reactjs babel
1个回答
0
投票

所以,我找出了问题@ babel / plugin-proposal-decorators应该在@ babel / plugin-proposal-class-properties之前出现

{
  "presets": ["@babel/preset-env"],
  "plugins": [
    ["@babel/plugin-transform-react-jsx"],
    "@babel/plugin-transform-modules-commonjs",
    "@babel/plugin-syntax-dynamic-import",
    "@babel/plugin-proposal-object-rest-spread",
    "@babel/plugin-proposal-function-bind",
    [
      "@babel/plugin-proposal-decorators",
      {
        "legacy": true
      }
    ],
    "@babel/plugin-proposal-class-properties"
  ]
}

© www.soinside.com 2019 - 2024. All rights reserved.