2016-08-31 37 views
1

我在我的項目中使用reactjs路由器,並且我注意到我的所有URL都在最後使用斜槓,因此刷新頁面重定向到我沒有的服務器。路由沒有服務器端渲染和基址名稱硬編碼

此外,重定向與路徑而不是名稱一起使用,所以每次我更改基礎URL時,都必須更改所有重定向和路由初始化。

ReactRouter.browserHistory.push('/prefix/newRoute/'); 

ReactDOM.render(
     React.createElement(ReactRouter.Router, {history: ReactRouter.browserHistory}, 
     React.createElement(ReactRouter.Route, {path: '/', component: AppController}), 
     React.createElement(ReactRouter.Route, {component: LayoutController}, 
      React.createElement(ReactRouter.Route, {path: '/dashboard', component: DashboardController}) 
     ) 
    ) 
, document.getElementById('content')); 

這可能在URL中使用散列而不是斜槓嗎?這可能使用名稱重定向嗎?

+0

如果您使用的是browserHistory,那麼您需要一臺服務器將非資源GET請求重定向到index.html。如果你沒有/想要,那麼改爲基於散列的歷史記錄。 –

回答

3

只是使用hashHistory而不是browserHistory,你會很開心。

所以不是

ReactRouter.browserHistory.push('/prefix/newRoute/'); 

您將使用

ReactRouter.hashHistory.push('/prefix/newRoute/'); 

代替

React.createElement(ReactRouter.Router, {history: ReactRouter.browserHistory} 

,你會使用

React.createElement(ReactRouter.Router, {history: ReactRouter.hashHistory}