React Webpack“意外令牌”命名的箭头函数

IT技术 reactjs webpack ecmascript-6
2021-05-28 13:14:43

我已经使用 webpack-middleware 设置了 react fullstack 环境。我的代码中有一些 es6 语法,但是在没有构造函数或命名箭头函数的状态下出现错误。例如,我想对react排序表使用语义 UI:https : //react.semantic-ui.com/collections/table#table-example-sortable 编译时出现此错误: 在此处输入图像描述

我认为这是因为我在下面附上了错误的 webpack 设置。

const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
  entry: './client/index.js',
  output: {
    path: '/',
    filename: 'bundle.js'
  },
  module: {
    rules: [
      {
        use: 'babel-loader',
        test: /\.js$/,
        exclude: /node_modules/
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: 'client/index.html'
    })
  ]
};

.babelrc

{
  "presets": ["env", "react", "es2015"]
}
1个回答

您正在尝试使用类属性,这些属性目前处于第 3 阶段,作为类字段提案的一部分为了能够在今天使用它们,您必须安装babel-plugin-transform-class-properties.

npm install --save-dev babel-plugin-transform-class-properties

并将其添加到您的插件中.babelrc

{
  "presets": ["env", "react"],
  "plugins": ["transform-class-properties"]
}

我还删除了es2015预设,因为它已被弃用而支持babel-preset-env,其中包含所有es2015功能等等。