Webpack / React.js 中的语法错误 React 中的意外令牌 = { }

IT技术 reactjs webpack babeljs jsx webpack-2
2021-05-28 05:45:24

在我的 .js 文件之一中,我从 Webpack 收到以下错误:

  4 | export default function(ComposedComponent) {
  5 |   class Authentication extends Component {
> 6 |     static contextTypes = {
    |                         ^
  7 |       router: React.PropTypes.object
  8 |     }
  9 | 

这些是我的devdependencies:

  "devDependencies": {
    "babel-core": "^6.17.0",
    "babel-loader": "^6.2.0",
    "babel-plugin-transform-object-rest-spread": "^6.26.0",
    "babel-preset-env": "^1.6.0",
    "babel-preset-react": "^6.16.0",
    "babel-preset-stage-3": "^6.24.1",
    "css-loader": "^0.26.1",
    "file-loader": "^1.1.4",
    "html-webpack-plugin": "^2.30.1",
    "image-webpack-loader": "^3.4.2",
    "rimraf": "^2.6.2",
    "style-loader": "^0.13.1",
    "url-loader": "^0.5.9",
    "webpack": "2.2.0-rc.0",
    "webpack-dev-server": "^2.2.0-rc.0"
  }

这是我的 Webpack 配置:

{
  "presets": ["env", "react", "stage-3"],
  "plugins": ["transform-object-rest-spread"]
}

有人能告诉我缺少哪个插件或包吗?谢谢!

2个回答

什么都没有丢失,只是语法不正确。

静态方法应该是(顾名思义)函数。

尝试:

class Authentication extends Component {
   ...
}

Authentication.contextTypes = {
    router: React.PropTypes.object
}

类属性目前是第 2 阶段的提案,因此未包含在babel-preset-env. 您可以:

  • 仅包含此特定转换.babelrc

    {“插件”:[“转换类属性”]}

  • 包括所有第 2 阶段的提案:

    { "预设": ["env", "react", "stage-2"] }

请在添加所有stage-2提案之前考虑风险https : //babeljs.io/docs/plugins/preset-stage-2/