2016-07-23 103 views
1

編譯時做出反應的錯誤,我有以下幾點:用的WebPack

import React from 'react'; 
import Router from 'react-router'; 
import { DefaultRoute, Link, Route, RouteHandler } from 'react-router'; 

import LoginHandler from './components/Login.js'; 

let App = React.createClass({ 
    render() { 
    return (
     <div className="nav"> 
     <Link to="app">Home</Link> 
     <Link to="login">Login</Link> 

     {/* this is the importTant part */} 
     <RouteHandler/> 
     </div> 
    ); 
    } 
}); 

let routes = ( 
    <Route name="app" path="/" handler={App}> 
    <Route name="login" path="/login" handler={LoginHandler}/> 
    </Route> 
); 

Router.run(routes, function (Handler) { 
    React.render(<Handler/>, document.body); 
}); 
當我運行 「NPM啓動」 基於我的配置

var webpack = require('webpack'); 


module.exports = { 
    entry: [ 
     'webpack/hot/only-dev-server', 
     "./js/app.js" 
    ], 
    output: { 
     path: __dirname + '/build', 
     filename: "bundle.js" 
    }, 
    module: { 
     loaders: [ 
      { test: /\.js?$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/ }, 
      { test: /\.js$/, exclude: /node_modules/, loader: 'babel-loader'}, 
      { test: /\.css$/, loader: "style!css" } 
     ] 
    }, 
    plugins: [ 
     new webpack.HotModuleReplacementPlugin(), 
     new webpack.NoErrorsPlugin() 
    ] 

}; 

我收到以下錯誤:

./js/app.js 
Module build failed: SyntaxError: /Users/alessandrosantese/Documents/PROJECTS/react-router-16/js/app.js: Unexpected token (10:6) 
    8 | render() { 
    9 |  return (
> 10 |  <div className="nav"> 
    |  ^
    11 |   <Link to="app">Home</Link> 
    12 |   <Link to="login">Login</Link> 
    13 | 

回答

2

我想你想讓.js.jsx都通過react-hotbabel,但你的模式匹配.j.js(我認爲你認爲?是通配符,但在正則表達式這意味着「匹配前面的表達式0或1次」)。

試試這個:

test: /\.jsx?$/ 
1

我會建議你兩個變化。

第一:

react-hot,你需要transpile JSX babel loader您的WebPack。所以,你的測試改爲

{ test: /\.jsx?$/, loaders: ['react-hot', 'babel'], exclude: /node_modules/ }, 

要同時檢查.js.jsx

二:

代替React.render()使用ReactDOM.render()

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import Router from 'react-router'; 
import { DefaultRoute, Link, Route, RouteHandler } from 'react-router'; 

import LoginHandler from './components/Login.js'; 

let App = React.createClass({ 
    render() { 
    return (
     <div className="nav"> 
     <Link to="app">Home</Link> 
     <Link to="login">Login</Link> 

     {/* this is the importTant part */} 
     <RouteHandler/> 
     </div> 
    ); 
    } 
}); 

let routes = ( 
    <Route name="app" path="/" handler={App}> 
    <Route name="login" path="/login" handler={LoginHandler}/> 
    </Route> 
); 

Router.run(routes, function (Handler) { 
    ReactDOM.render(<Handler/>, document.body); 
});