2016-01-27 11 views
7

ネストされたURLルーティングを実現するためにreact-routerとwebpack-dev-serverでいくつかの問題があります。react-routerとwebpack dev serverを使用したネストされたURLルーティング

webpack.config.js

output: { 
    path: path.resolve(__dirname, 'build'), 
    publicPath: "/", <-- this enabled routing to /register/step2 
    filename: "js/bundle.js", 
}, 

routes.js appliationに周りをクリックすると、私は/登録/ STEP2に私はリフレッシュを打つ一度取得することができます

const routes = { 
    childRoutes: [ 
     { path: '/', component: Home }, 
     { path: '/login', component: Login }, 
     { path: '/register', component: Register }, 
     { path: '/register/step2', component: SecondStep }, 
    ] 
}; 

export default (<Router routes={routes} history={createBrowserHistory()} />); 

ブラウザでは、common.jsとbundle.jsが見つからない:404、/ register /ディレクトリからすべてをロードしようとしているので。

誰でも手助けできますか?ありがとう。

答えて

0

createBrowserHistory()の代わりにhashHistoryを使用すると、サーバーがサーバー上の現在のURLを要求しないようになります。

export default (<Router routes={routes} history={hashHistory} />); 
+0

ハッシュを使用せずにこれを達成する方法はありませんか? –

+0

反応ルータのガイドでは、browserHistory over hashHistoryを使用することを推奨しています。 https://github.com/reactjs/react-router/blob/1.0.x/docs/guides/basics/Histories.md#createbrowserhistory –

7

私はそれを理解しました。これを可能にするために必要な2つのこと。

webpack.config.js

devServer: { 
    historyApiFallback: true <-- this needs to be set to true 
} 


routes.js

const routes = { 
    childRoutes: [ 
     { path: '/', component: Home }, 
     { path: '/login', component: Login }, 
     { path: '/register', component: Register, childRoutes: [ 
      { path: 'step2', component: SecondStep }, 
     ] }, 
    ] 
}; 
+1

historyApiFallback:true、問題を解決するようです..ありがとう – pravin

+0

ホットリロード:http://stackoverflow.com/questions/40159965/enable-single-page-app-react-hot-reload-webpack – Marc

関連する問題