问题描述
我在 Django 项目中设置 React 时遇到了这个错误
While I was setting up React within Django project I came across this error
ModuleBuildError 在模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):语法错误:C:Users1SunCebula3cebula_reactassetsjsindex.js:支持对于实验性语法classProperties"当前未启用 (17:9):
ModuleBuildError in Module build failed (from ./node_modules/babel-loader/lib/index.js): SyntaxError: C:Users1SunCebula3cebula_reactassetsjsindex.js: Support for the experimental syntax 'classProperties' isn't currently enabled (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
So, I installed @babel/plugin-proposal-class-properties and put this in 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"
]
}
但是错误依旧存在,请问是什么问题?
However the error is still existed, What is the problem??
推荐答案
改变
"plugins": [
"@babel/plugin-proposal-class-properties"
]
收件人
"plugins": [
[
"@babel/plugin-proposal-class-properties",
{
"loose": true
}
]
]
这对我有用
这篇关于当前未启用对实验语法“classProperties"的支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!