简体   繁体   English

我不能在Babel的webpack中使用rest运算符

[英]I cannot use the rest operator in webpack with Babel

I'm using webpack and React for my app, and for some reason webpack is not liking var {id, ...data} = pulse; 我正在为我的应用程序使用webpack和React,由于某种原因,webpack不喜欢var {id, ...data} = pulse; .

Error: 错误:

ERROR in ./src/main.js
Module build failed: SyntaxError: Unexpected token (122:13)

  120 |   },
  121 |   editPulse: function(pulse) {
> 122 |     var {id, ...data} = pulse;
      |              ^
  123 |     console.log('Calling editPulse API: ');
  124 |     console.log(id, data);
  125 |     console.log(JSON.stringify(data));

My webpack.config.js looks like this: 我的webpack.config.js看起来像这样:

module.exports = {
  ...snip...
  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        exclude: /node_modules/,
        loader: 'babel',
        query: {
          presets: ['es2015', 'react']
        }
      }
    ]
  }
};

And through npm, my package.json is: 通过npm,我的package.json是:

{
  ...snip...
  "devDependencies": {
    "babel-core": "^6.16.0",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.16.0",
    "babel-preset-react": "^6.16.0",
    "css-loader": "^0.25.0",
    "style-loader": "^0.13.1",
    "webpack": "^1.13.2"
  },
  "dependencies": {
    "babelify": "^7.3.0",
    "jquery": "^3.1.1",
    "react": "^15.3.2",
    "react-addons-update": "^15.3.2",
    "react-bootstrap": "^0.30.3",
    "react-dom": "^15.3.2"
  }
}

Like Jonathan Lonowski said above, you'll need to npm install one of the babel-preset-stage-(0-3 here) in order to use the Rest notation/Es2015 object spread notation. 就像上面的Jonathan Lonowski所说的那样,您需要npm安装babel-preset-stage-(此处为0-3)之一才能使用Rest符号/ Es2015对象散布符号。

Don't forget to add the preset to your webpack.config.js file too. 也不要忘记将预设添加到您的webpack.config.js文件中。 So if you installed the stage-2 babel preset your config file should have babel-preset-stage-2 as an additional entry under: module: loaders: query 因此,如果您安装了stage-2 babel预设,则您的配置文件应具有babel-preset-stage-2作为以下项的附加条目:module:loaders:query

From @Jonathan Lonowski's comment , I installed the babel-preset-stage-3 to add support to use the rest operator on Objects. 根据@Jonathan Lonowski的评论 ,我安装了babel-preset-stage-3来添加对在Objects上使用rest运算符的支持。

npm install babel-preset-stage-3 --save-dev

Then update the webpack.config.js to include that preset: 然后更新webpack.config.js以包含该预设:

presets: ['react', 'es2015', 'stage-3']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM