环境
React 16.13
出错信息:
出错的原因:
Support for the experimental syntax ‘classProperties’ isn’t currently enabled
解决方式 一:
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the ‘plugins’ section of your Babel config to enable transformation.
解决方式 二:
If you want to leave it as-is, add @babel/plugin-syntax-class-properties (https://git.io/vb4yQ) to the ‘plugins’ section to enable parsing
解决
采用方案一:
npm install --save-dev @babel/plugin-proposal-class-properties
修改 webpack的配置文件(我的是webpack.config.js
)
在babel-loader
的配置下新增plugins
配置项:
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
use: {
loader: 'babel-loader',
options: {
presets: [
'@babel/preset-env',
'@babel/preset-react',
],
plugins: [
"@babel/plugin-proposal-class-properties",
]
}
}
},
babel-loader 详细配置项见: https://www.webpackjs.com/loaders/babel-loader/
webpack配置文件修改后,重启应用即可。