reactjs - 目前尚未启用对实验性语法 'classProperties' 的支持

标签 reactjs webpack babeljs

当我在 Django 项目中设置 React 时,我遇到了这个错误

模块构建错误 模块构建失败(来自 ./node_modules/babel-loader/lib/index.js): 语法错误:C:\Users\1Sun\Cebula3\cebula_react\assets\js\index.js:支持 对于实验性语法“classProperties”当前未启用 (17:9):

  15 | 
  16 | class BodyPartWrapper extends Component {
> 17 |   state = {
     |         ^
  18 | 
  19 |   }
  20 | 

Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 
'plugins' section of your Babel config to enable transformation.

所以,我安装了 @babel/plugin-proposal-class-properties 并将其放入 babelrc

package.json

{
  "name": "cebula_react",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "webpack-dev-server --config ./webpack.config.js --mode development",
    "test": "echo \"Error: no test specified\" && exit 1",
    "build": "webpack --config prod.config.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "babel": {
    "presets": [
      "@babel/preset-env",
      "@babel/preset-react"
    ]
  },
  "devDependencies": {
    "@babel/cli": "^7.0.0",
    "@babel/core": "^7.0.0",
    "@babel/plugin-proposal-class-properties": "^7.0.0",
    "@babel/preset-env": "^7.0.0",
    "@babel/preset-react": "^7.0.0",
    "babel-loader": "^8.0.2",
    "babel-plugin-transform-class-properties": "^6.24.1",
    "react-hot-loader": "^4.3.6",
    "webpack": "^4.17.2",
    "webpack-bundle-tracker": "^0.3.0",
    "webpack-cli": "^3.1.0",
    "webpack-dev-server": "^3.1.8"
  },
  "dependencies": {
    "react": "^16.5.0",
    "react-dom": "^16.5.0"
  }
}

babelrc

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "@babel/plugin-proposal-class-properties"
  ]
}

但是错误仍然存​​在,请问是什么问题?

最佳答案

改变

"plugins": [
    "@babel/plugin-proposal-class-properties"
  ]

"plugins": [
    [
      "@babel/plugin-proposal-class-properties",
      {
        "loose": true
      }
    ]
  ]

这对我有用

关于reactjs - 目前尚未启用对实验性语法 'classProperties' 的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52237855/

相关文章:

javascript - Reactjs:如何确保用户不会修改 POST 请求的响应

javascript - 在 React.render() 中返回多个元素

javascript - 递归函数如何将消息保存在数组中?

reactjs - 如果本地提供index.html,可以使用babel-standalone吗? (没有网络服务器)

node.js - 在 Webpack 中,如何动态设置公共(public)路径?

node.js - 如何将nextjs部署到非根目录的目录中

javascript - 如何在 vuejs webpack cli 项目中包含 jquery?

node.js - ES2015 模块无法在 Node.js 和 Babel.js 中工作?

javascript - ES6 可以安全地使用静态类变量作为 Map 的键吗?

javascript - 从模块中导出 { default } 不适用于 Jest 中的 Babel 7 和 preset-env