Arrow Function syntax not working with webpack?

后端 未结 5 773
渐次进展
渐次进展 2020-11-28 11:10

I\'m making an app on react-redux. I\'m using webpack for bundling and babel for transpiling. When I am try to use arrow function in my code. It gives me error as :

5条回答
  •  伪装坚强ぢ
    2020-11-28 11:50

    Stab in the dark, is this function inside a class? Arrow functions that are members of a class are not included in ES2015 (or 2016). If you want to do something like:

    class Foo {
      bar = (baz) => {
        console.log(baz);
      } 
    }
    

    You'll need to include babel-transform-class-properties.

    In your example, you'll need to:

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

    and change your loader to

    {
            test: /\.js$/,
            loader: 'babel-loader',
            exclude: /node_modules/,
            query: {
              presets: ['react', 'es2015', 'react-hmre'],
              plugins: ['transform-class-properties']
            }
          }
    

提交回复
热议问题