React - ES6 - 意外的令牌
React - ES6 - Unexpected token
我遇到了这个错误
ERROR in ./src/components/Header/index.js
Module build failed: SyntaxError: C:/Users/Gil/Documents/Projects/ecommerce/src/components/Header/index.js: Unexpected token (16:7)
14 | }
15 |
> 16 | test = () => {
| ^
17 | console.log('pass!');
18 | };
19 |
我认为它与 ES6 有关,但我不知道,无论如何这是我的配置
webpack.config.js
loaders: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader'
},...
package.json
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react-scripts": "1.0.7"
所以我的问题是,当我尝试使用 arrow function
时出现此错误,我需要额外的配置还是我做错了什么?
你需要https://babeljs.io/docs/plugins/transform-class-properties/
npm install --save-dev babel-plugin-transform-class-properties
和内部 .babelrc
:
"plugins": ["transform-class-properties"]
我遇到了这个错误
ERROR in ./src/components/Header/index.js
Module build failed: SyntaxError: C:/Users/Gil/Documents/Projects/ecommerce/src/components/Header/index.js: Unexpected token (16:7)
14 | }
15 |
> 16 | test = () => {
| ^
17 | console.log('pass!');
18 | };
19 |
我认为它与 ES6 有关,但我不知道,无论如何这是我的配置
webpack.config.js
loaders: [
{ test: /\.js$/, loader: 'babel-loader', exclude: /node_modules/ },
{ test: /\.jsx$/, loader: 'babel-loader', exclude: /node_modules/ },
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'eslint-loader'
},...
package.json
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react-scripts": "1.0.7"
所以我的问题是,当我尝试使用 arrow function
时出现此错误,我需要额外的配置还是我做错了什么?
你需要https://babeljs.io/docs/plugins/transform-class-properties/
npm install --save-dev babel-plugin-transform-class-properties
和内部 .babelrc
:
"plugins": ["transform-class-properties"]