【问题标题】:React Router (v3) redirect not redirecting to new locationReact Router(v3)重定向不重定向到新位置
【发布时间】:2016-02-07 09:26:24
【问题描述】:

我有这样的路由器设置:

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 的白页。

在文档中似乎找不到任何关于此的内容。路线的“来自”部分是否仍然必须存在才能使其正常工作,还是无论如何都应该重定向我?

编辑:

我还要提一下,我使用了 history 包,正如 react-router 升级指南中提到的那样: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);

【问题讨论】:

    标签: reactjs react-router webpack-dev-server


    【解决方案1】:

    原来我的路线顺序是错误的。我的“catch all” NotFound-route 需要放在我的重定向之后才能正常工作。

    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;
    

    【讨论】:

      【解决方案2】:

      好像你有一个错字。在您的路线中,您有&lt;Route path="about" component={About} /&gt;,请注意您有path="about",并且您正在尝试导航到/about-us。这与您的任何路线都不匹配。尝试导航到/about 或将您的路线更改为&lt;Route path="about-us" component={About} /&gt;

      【讨论】:

      • 谢谢!我想我用/ 尝试了你的第一个建议,但明天我回去工作时再看看。与此同时,文档/变更日志怎么没有提到这一点(你的第二个建议,指的是component={About})? github.com/rackt/react-router/blob/master/upgrade-guides/…(参见重定向部分):)
      • 所以,我已经尝试了您的建议,但它不起作用:(看起来重定向通常是一个问题,由于某种原因。我也尝试在现有页面之间重定向,但没有运气. 还是得到白页说cannot GET /component-name
      • 我终于想通了。事实证明,我的'catch all' NotFound 路由需要在我的重定向之后定义才能工作:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 2015-09-14
      • 2022-01-22
      • 2018-07-15
      • 2022-06-24
      • 1970-01-01
      相关资源
      最近更新 更多