gpt4 book ai didi

reactjs - React Router (v3) 重定向不重定向到新位置

转载 作者:行者123 更新时间:2023-12-03 14:15:09 24 4
gpt4 key购买 nike

我的路由器设置如下:

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

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

编辑:

我还应该提到,我使用了历史包,如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);

最佳答案

原来我的路线顺序是错误的。我的“捕获所有”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;

关于reactjs - React Router (v3) 重定向不重定向到新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35251783/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com