2016-08-20 52 views
0

我有以下途徑:重定向路線編程方式使用路線名稱

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

這是我的重定向應用程序流:

ReactRouter.browserHistory.push('/dashboard'); 

它的工作原理,但如果我打開我的應用程序上的另一機器具有不同的網址結構,並且我必須更改網址路徑,我也必須更改每個push參數。

有沒有辦法使用路由名稱而不是路徑重定向?

+1

我不明白這樣的路徑在另一臺機器上會有什麼不同。如果是這種情況,您是否還必須在路線中配置不同的路徑?順便說一句,這可能不是配置路線的理想方式。如果你不想用JSX來做,我會建議使用['PlainRoute'](https://github.com/reactjs/react-router/blob/69b163255ca6fc363fe270911b1161378650cc1b/docs/API.md#plainroute)對象寫出'React.createElement()'調用。 – JMM

回答

0

在舊版本中可以按名稱導航,但AFAIK它不再有效。在我看來,你應該做的是爲不同的服務器配置歷史記錄的基本名稱,即路由的前綴。考慮到URL的最後部分,即由react-router配置的URL是固定的。

爲了指定基準名稱,您應該將自定義歷史記錄傳遞給路由器。

useRouterHistory(createBrowserHistory)({ basename })