【问题标题】:Getting an uxepected token error using reactjs and nodejs使用 reactjs 和 nodejs 获取 uxepected 令牌错误
【发布时间】:2017-10-01 13:32:24
【问题描述】:

当创建一个计算点击次数的简单按钮时,我在按钮标签上得到了意外的令牌错误(我可以说)没有原因

import React from 'react';

/**
 * A counter button: tap the button to increase the count.
 */
class Counter extends React.Component {
  constructor() {
    super();
    this.state = {
      count: 0,
    };
  }

  render() {
    return (
      <button onClick={() => {this.setState({ count: this.state.count + 1 });}}>
        Count: {this.state.count}
      </button>
    );
  }
}
export default Counter;

【问题讨论】:

  • 您的代码在我的系统上运行良好。你能发布你得到的确切错误吗?
  • @LuiGiovanni 你在编译吗?
  • 不,我正在编译
  • 你需要使用像 babel 这样的 ES6 to ES5 transpiler 将你用 ES6 编写的 React 代码转译成 ES5,这样浏览器才能理解 JS 代码。

标签: node.js reactjs


【解决方案1】:

您可能在 webpack.config.js 中缺少预设。 试试这个

module : {
        rules:[
            { 
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use : {
                    loader: "babel-loader",
                    options: {
                        presets: ['env','react']
                    }
                }
            }
        ]
}

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签