javascript - React.js 语法错误

标签 javascript reactjs react-router

我已经完成了 React/Elixir“learnphoenix.io”教程,现在正在处理路由和 View 部分。我有一个编译错误

ERROR in ./app/index.js
Module build failed: SyntaxError: Unexpected token (6:22)

  4 | import { default as Home } from   "./components/Home"
  5 | import { default as Settings } from   "./components/Settings"
> 6 | const App =   props   => (<div>{props.children}</div>)
    |                         ^
  7 | ReactDOM.render(
  8 |       <Router history={hashHistory}>
  9 |               <Route path="/" component={App}>

 @ multi main
webpack: bundle is now VALID.

我是 React 新手,所以不太了解。

编辑1: 有 webpack.config.js :

var path = require('path')
var webpack = require('webpack')

module.exports = {
  devtool: 'eval',
  entry: [
    'webpack-dev-server/client?http://localhost:3000',
    './app/index'
  ],
  output: {
    path: path.join(__dirname, 'dist'),
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /\.js$/,
        loader: 'babel',
        exclude: /node_modules/,
        query: {
          presets: ['es2015', 'babel']
        },
        include: path.join(__dirname, 'app')
      }
    ]
  },
  resolve: {
    extensions: [ '', '.js' ]
  }
}

应用程序/index.js:

import React from "react"
import ReactDOM from "react-dom"
import { Router, Route, IndexRoute, hashHistory }   from "react-router"
import { default as Home } from "./components/Home"
import { default as Settings } from "./components/Settings"
const   App =   props   => (<div>{props.children}</div>)
ReactDom.render(
        <Router history={hashHistory}>
                <Route path="/" component={App}>
                        <IndexRoute component={Home}/>
                        <Route path="settings"  component={Settings}/>
                </Route>
        </Router>,
        document.getElementById("root")
)

app/Home/index.js:

import React from "react"

export class Home extends React.Component {
  render() {
    return (<div>Home component</div>)
  }
}

export default Home

最佳答案

这是正确的 jsx ES6 语法,但它看起来像是保存为 .js。

您需要在构建过程中进行转译步骤,可能需要使用 Babel

关于javascript - React.js 语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42026646/

相关文章:

javascript - 连接对象不起作用或者我需要将值转换为正确的格式?

javascript - 如何解构这个数组

javascript - 从数组中间获取相等填充的键

javascript - 仅在 Promise 解决后才执行函数语句

html - 使用 Nginx 响应路由器 html5 Pushstae

javascript - jQuery.each 用于列表和非列表

reactjs - 奇怪的 IE11 行为 "Object doesn' t 支持属性或方法 'includes' ”

reactjs - 部署使用 Vercel 或 Nextjs 重写配置的项目后找不到 Assets 路径

javascript - 如何使用React前端和express后端从url获取参数?

reactjs - React-router : How to trigger $(document). 准备好了吗?