【发布时间】:2017-10-07 15:05:34
【问题描述】:
我在我的 ReactJS 应用程序上获得了 Unexpected Token。但我相信语法是正确的。
import React, { Component } from 'react';
class Auth extends Component {
constructor(){
super()
this.state={
authStatus: "Sign In",
isAuthenticated: false
}
}
AuthMe = () =>{
console.log("Working")
}
render() {
return (
<button type="button" onClick={this.AuthMe}>{this.state.authStatus}</button>
);
}
}
export default Auth;
./src/components/Auth/index.js 中的错误 模块构建失败:SyntaxError: Unexpected token (11:11)
> 11 | AuthMe = () =>{
| ^
12 | console.log("Working")
13 | }
我错过了什么?
更新
这是我的 webpack.config.js
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
publicPath: '/',
},
module: {
rules: [{
test: /\.jsx?$/,
exclude: [
path.resolve(__dirname, 'node_modules'),
],
loader: 'babel-loader',
}]
},
resolve: {
extensions: ['.json', '.js', '.jsx', '.css']
},
devtool: 'source-map',
devServer: {
historyApiFallback: true
}
};
.babelrc
{
"presets": ["es2015","react","stage-0"],
"plugins": ["transform-decorators-legacy"]
}
【问题讨论】:
-
你能展示你如何配置你的 webpack
-
请稍等
-
@SaurabhSharma .babelrc ?
-
@ShubhamKhatri 更新
-
所以你的代码肯定是有效的,在Babel REPL中编译得很好。看起来与您的 webpack 配置有关。 - 你能告诉我你的文件结构吗? package.json 也是?
标签: javascript reactjs ecmascript-6