我使用了react-router
,甚至尝试过<Redirect .. />
,我都无法重定向到/
,主要问题是/
不在其他路线上,所以我需要使用<Redirect from="*" to="" />
,似乎不工作。
然后,我还需要防止/main
重定向到/
,我将<Redirect .. >
放在<Route component={MainView} path="main">
另一个要求,如何使localhost:3000
到localhost:3000/
使用AppIndex IndexRoute
请检查下面的代码:
import MainView from 'components/MainView';
import ThemeIndex from 'components/ThemeIndex';
import AppIndex from 'components/ThemeIndex';
import Shop from 'components/Shop';
import App from 'components/App';
export default (
<Route component={App} path="/">
<IndexRoute component={AppIndex}></IndexRoute>
<Route component={MainView} path="main">
<IndexRoute component={ThemeIndex}></IndexRoute>
</Route>
<Redirect from="*" to="" />
</Route>
);
有任何想法吗?
而不是离开 to
一个空字符串,您必须指定路径,在这种情况下,这意味着<Redirect from="*" to="/" />
。
要使Localhost:3000到Localhost:3000/,您无需做任何事情,您当前的代码已经完成了工作。如果您转到任何网站的根网址,请说http://stackoverflow.com,然后尝试使用location.pathname
键入控制台,您将获得"/"
请问我是否还有其他问题。
同意大卫所说的话,如果有其他用例