javascript - React-router 自动将任何路由重定向到 root "/"除了 "/main"

标签 javascript reactjs routes react-router

我用了react-router , 甚至尝试过 <Redirect .. /> , 我无法重定向到 / ,主要问题是 /在其他路线之外,所以我需要使用<Redirect from="*" to="" /> ,好像不行。

那我还需要防止/main重定向到 / , 我把 <Redirect .. ><Route component={MainView} path="main"> 之后

另一个需求,如何生成localhost:3000localhost: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/

相关文章:

javascript - AngularJS:用户可以注册两次,如何防止这种情况?

javascript - 如何将状态从子组件传递到应用程序组件

javascript - Netlify 正在显示我的 Gatsby 网站的 html 版本

routes - 带有语言键的 404 页面的 Gatsby 路由问题

linux - 从 wifi 访问以太网 ip

python - 有没有办法检查实时流量 scapy 中的数据包?

javascript - 检测元素在屏幕上的百分比

javascript - Chrome 扩展程序 - setInterval 在 popup.html 关闭时停止

javascript - Locomotive JS - 如何访问另一个 Controller 方法

reactjs - 传递函数调用来处理嵌套状态更改。我在这里做错了什么?