【问题标题】:Why do I get a syntax error when using a spread operator? [duplicate]为什么在使用展开运算符时会出现语法错误? [复制]
【发布时间】:2017-06-02 17:46:18
【问题描述】:

我使用 ES6 的扩展运算符和 Babel 作为 ES5 的转译器。当我像这样使用扩展运算符时:

const { height, weight, radius, ...otherValues } = sphere;

我收到SyntaxError

ERROR in ./src/sphere.js
Module build failed: SyntaxError: Unexpected token (7:36)

   6 | 
>  7 |   const { height, weight, radius, ...otherValues } = sphere;
     |                                   ^
   8 | 

这是为什么?

【问题讨论】:

标签: javascript ecmascript-6 babeljs


【解决方案1】:

Babel 不会将扩展运算符转译为开箱即用。您还需要为 Babel 安装 babel-plugin-transform-object-rest-spread 插件。安装该软件包后,您的代码应该无需进一步更改即可编译。

【讨论】:

  • 您还需要安装舞台预设。 "npm install --save-dev babel-preset-stage-0",如果有babelrc文件,添加stage-0预设。
猜你喜欢
  • 1970-01-01
  • 2013-11-09
  • 2019-02-05
  • 2020-02-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多