【问题标题】:React - ES6 - Unexpected token [duplicate]React - ES6 - 意外的令牌[重复]
【发布时间】:2017-11-05 17:01:56
【问题描述】:

我收到了这个错误

ERROR in ./src/components/Header/index.js
Module build failed: SyntaxError: C:/Users/Gil/Documents/Projects/ecommerce/src/components/Header/index.js: Unexpected token (16:7)

  14 |   }
  15 |
> 16 |   test = () => {
     |        ^
  17 |     console.log('pass!');
  18 |   };
  19 |

我认为它与 ES6 相关,但我不知道,反正这是我的配置

webpack.config.js

loaders: [
      { test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
      { test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
      {
        test: /\.js$/,
        exclude: /node_modules/,
        loader: 'eslint-loader'
      },...

package.json

"devDependencies": {
    "babel-core": "^6.24.1",
    "babel-loader": "^7.0.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-preset-react": "^6.24.1",
    "react-scripts": "1.0.7"

所以我的问题是,当我尝试使用 arrow function 时出现此错误,我需要额外的配置还是我做错了什么?

【问题讨论】:

  • “当我尝试使用箭头函数时出现此错误” 问题不在于箭头函数。这是与作业test = ...;
  • 谢谢@FelixKling,我刚刚标记了它。

标签: reactjs webpack babeljs ecmascript-next


【解决方案1】:

你需要https://babeljs.io/docs/plugins/transform-class-properties/

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

.babelrc内部:

"plugins": ["transform-class-properties"]

【讨论】:

  • 你可能想解释一下原因。
  • 此外,还有一些预设,不仅包括此建议的语言功能,还包括其他功能。例如,stage-2 预设当前包括所有语言特性,这些特性已经达到了包含在 ECMAScript 中的第 2 阶段(共 4 个)。
猜你喜欢
  • 1970-01-01
  • 2018-09-13
  • 2019-03-21
  • 2017-02-01
  • 1970-01-01
  • 2019-01-11
  • 2016-05-30
  • 2018-01-20
  • 2016-07-01
相关资源
最近更新 更多