【问题标题】:React Webpack "unexpected token" named arrow functionReact Webpack“意外令牌”命名箭头函数
【发布时间】:2018-03-28 02:55:03
【问题描述】:

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

我以为是因为 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"]
}

【问题讨论】:

标签: reactjs webpack ecmascript-6


【解决方案1】:

您正在尝试使用作为Class fields proposal 一部分的当前第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 所做的一切以及更多。

【讨论】:

  • 这些不是类字段,但它们是“胖箭头”风格的方法?
  • 它们是类属性,你只是碰巧为它分配了一个箭头函数。您也可以使用hideFixedMenu = true 或其他任何东西来代替true。另请参阅Example shown by babel-plugin-transform-class-properties
  • 感谢大家的快速帮助。你太棒了。
猜你喜欢
  • 2016-07-23
  • 2017-07-22
  • 2020-01-29
  • 2016-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
相关资源
最近更新 更多