Reactjs 网页包预设时缺少类属性转换:“0”;环境";

Reactjs 网页包预设时缺少类属性转换:“0”;环境";,reactjs,webpack,babeljs,babel-preset-env,Reactjs,Webpack,Babeljs,Babel Preset Env,我正在Web包开发服务器上测试React项目。我想使用,但在state init处出错 client:162 ./src/containers/App.js Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: Missing class properties transform. 2 | 3 | class App extends Component { 4 | state

我正在Web包开发服务器上测试React项目。我想使用,但在state init处出错

client:162 ./src/containers/App.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Missing class properties transform.
2 | 
3 | class App extends Component {
4 |     state = {
  |     ^
5 |         count: 0
6 |     }
7 |     // constructor(props) {
我找到了一种解决方案,将预设设置为这一
“预设”:[“es2015”、“0级”、“反应”]
。但我使用的是“环境”预设。我认为“环境”预设支持所有巴贝尔阶段。我必须更改预设吗

App.js

class App extends Component {

state = {
    count: 0
}

render() {
    return (
      <div>
          <input defaultValue={this.state.count} />
      </div>
    );
  }
}

export default App;
B.法律改革委员会

{
  "presets": ["env", "react"],
  "plugins": ["transform-class-properties"]
}
.webpack.config.js

const webpack = require('webpack');
const path = require('path');

module.exports = {
    mode: 'development',
    entry: './src/index.js',
    output: {
        path: path.resolve(__dirname, 'dist'),
        publicPath: '/dist/',
        filename: 'bundle.js'
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                include: path.join(__dirname),
                exclude: /(node_modules)|(dist)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['env']
                    }
                }
            }
        ]
    }
}
试试这个:

.babelrc

{
  "presets": ["env", "react", "stage-2"]
}
package.json

"devDependencies": {
  "babel-cli": "^6.26.0",
  "babel-core": "^6.26.3",
  "babel-loader": "^7.1.4",
  "babel-preset-env": "^1.7.0",
  "babel-preset-react": "^6.24.1",
  "babel-preset-stage-2": "^6.24.1",
  ...
使用
.babelrc
文件时,在
webpack.config.js
中,我们可以简单地如下配置:

{
   test: /\.js$/,
   use: ['babel-loader']
}

希望这能有所帮助。

谢谢,它很管用!但我很好奇为什么第二阶段是必要的。
{
   test: /\.js$/,
   use: ['babel-loader']
}