无法使箭头功能起作用(React,Babel,Webpack,ES6)

时间:2017-02-04 22:44:47

标签: reactjs webpack babeljs

绝望地希望我能找到一个可以说明为什么我无法使用箭头功能来处理我的Webpack / Babel设置的人,尝试了很多东西,到目前为止还没有任何工作。项目现状:

的package.json:

{
  "name": "..",
  "version": "1.0.0",
  "description": "..",
  "main": "app.jsx",
  "author": "..",
  "license": "ISC",
  "devDependencies": {
    "babel-core": "^6.22.1",
    "babel-loader": "^6.2.10",
    "babel-plugin-transform-class-properties": "^6.22.0",
    "babel-plugin-transform-react-jsx": "^6.22.0",
    "babel-preset-es2015": "^6.22.0",
    "babel-preset-react": "^6.22.0",
    "babel-preset-stage-2": "^6.22.0",
    "css-loader": "^0.26.1",
    "node-sass": "^4.5.0",
    "sass-loader": "^4.1.1",
    "style-loader": "^0.13.1",
    "webpack": "^2.2.1",
    "webpack-dev-server": "^2.3.0"
  },
  "dependencies": {
    "lodash": "^4.17.4",
    "react": "^15.4.2",
    "react-dom": "^15.4.2",
    "react-router": "^3.0.2"
  }
}

webpack.config.js:

var path = require('path');

module.exports = {
  entry: './src/router.jsx',
  output: {
    filename: 'bundle.js',
    path: path.resolve(__dirname, 'public')
  },
  watch: true,
  module: {
    loaders: [
      {
         test: /\.jsx$/,
         exclude: /(node_modules)/,
         loader: 'babel-loader',
         query: {
           presets: ['react', 'es2015']
         }
      },
      {
        test: /\.scss$/,
        loader: 'style-loader!css-loader!sass-loader'
      },
    ]
  }
};

.babelrc

{
  "plugins": [
    "transform-react-jsx",
    "transform-class-properties"
  ],
  "presets": [
    "es2015",
    "react",
    "stage-2"
  ],
}

组件:

import React from 'react'

import './styles.scss'

class Button extends React.Component {
  asdf = () => {
    return ['btn', this.props.size].join(' ')
  }

  render() {
    return (
      <button className={this.asdf}>
        {this.props.children}
      </button>
    )
  }
}

export default Button

错误:

ERROR in ./src/app/ui-kit/button.jsx
Module build failed: SyntaxError: Missing class properties transform.

  4 |
  5 | class Button extends React.Component {
> 6 |   asdf = () => {
    |   ^
  7 |     return ['btn', this.props.size].join(' ')
  8 |   }
  9 |

 @ ./src/app/components/signup.jsx 13:14-45
 @ ./src/app/app.jsx
 @ ./src/router.jsx

1 个答案:

答案 0 :(得分:5)

查看我的babel软件包,.babelrc正在我当前的项目中进行比较并与您进行比较:

.babelrc:

{
  "presets": ["react", "es2015" , "stage-0"],
  "plugins": [
    ["transform-decorators-legacy"]
  ]      
}

packages.json

"babel-core": "^6.4.5",
"babel-eslint": "^6.1.2",
"babel-loader": "^6.2.1",
"babel-plugin-react-transform": "^2.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-preset-stage-0": "^6.3.13",