我用了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/,您无需执行任何操作,您当前的代码已经完成了工作。
如果你去任何网站的根 URL,说 http://stackoverflow.com , 然后尝试输入 location.pathname
在控制台中,你会得到 "/"
还有什么问题可以问我
关于javascript - React-router 自动将任何路由重定向到 root "/"除了 "/main",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37849514/