2016-02-07 51 views
1

我有一個路由器的設置是這樣的:陣營路由器(V3)重定向不重定向到新的位置

import React from 'react'; 
import {IndexRoute, NotFoundRoute, Route, Redirect} from 'react-router'; 

import App from './components/app'; 
import Home from './components/Home'; 
import AuthorPage from './components/authors/AuthorPage'; 
import About from './components/about/About'; 
import NotFound from './components/NotFound'; 

const routes = (
    <Route path="/" component={App} > 
     <IndexRoute component={Home} /> 
     <Route path="about" component={About} /> 
     <Route path="authors" component={AuthorPage} /> 
     <Route path="*" component={NotFound} /> 
     <Redirect from="/about-us" to="/about" /> 
    </Route> 
); 

export default routes; 

一切,但重定向工作正常。每當我嘗試導航到/about-us時,我都會看到一個白頁,顯示Cannot GET /about-us

似乎無法在文檔中找到任何關於此的信息。這條路線的「來自」部分是否仍然需要存在才能起作用,還是應該重定向我?

編輯:

我還要提到的是我用的是歷史包,作爲反應路由器升級指南中提到:https://github.com/rackt/react-router/blob/master/upgrade-guides/v1.0.0.md

這是我main.js:

import React from 'react'; 
import ReactDOM from 'react-dom'; 
import Router from 'react-router'; 
import routes from './routes'; 
import createBrowserHistory from 'history/lib/createBrowserHistory' 

// Using history to avoid default behavior of weird urls like `?_k=umhx1s` 
// See: https://github.com/rackt/react-router/blob/master/upgrade-guides/v1.0.0.md 
let history = createBrowserHistory(); 
const root = document.getElementById('app'); 

ReactDOM.render(<Router history={history}>{routes}</Router>, root); 

回答

1

原來我的路線順序錯了。在我的重定向之後,我的「全部捕獲」NotFound路由需要放置才能工作。

import React from 'react'; 
import {IndexRoute, NotFoundRoute, Route, Redirect} from 'react-router'; 

import App from './components/app'; 
import Home from './components/Home'; 
import AuthorPage from './components/authors/AuthorPage'; 
import About from './components/about/About'; 
import NotFound from './components/NotFound'; 

const routes = (
    <Route path="/" component={App} > 
    <IndexRoute component={Home} /> 
    <Route path="about" component={About} /> 
    <Route path="authors" component={AuthorPage} /> 
    <Redirect from="about-us" to="about" /> // Switched order with below Route. 
    <Route path="*" component={NotFound} /> 
    </Route> 
); 

export default routes; 
1

好像你在那裏有一個錯字。在您的路線中,您有<Route path="about" component={About} />通知您有path="about",並且您正在嘗試導航至/about-us。這不符合你的任何路線。嘗試導航至​​或將您的路線更改爲<Route path="about-us" component={About} />

+0

謝謝!我想我已經用'/'嘗試了你的第一個建議,但是明天當我重新開始工作時我會再看看。同時,docs/changelog怎麼沒有提到這個(你的第二個建議,指的是'component = {關於}')? https://github.com/rackt/react-router/blob/master/upgrade-guides/v1.0.0.md(見部分重定向):) –

+0

所以,我已經嘗試過你的建議,它不工作:(看起來像重定向一般是由於某種原因的問題,我已經嘗試重定向現有頁面之間,但沒有運氣。仍然得到白色頁面說'不能GET /組件名稱' –

+0

我終於明白了這一點原來我的'catch all'NotFound路由需要在我的重定向之後定義才能工作:) –