【问题标题】:RouterBrowser doesn't redirect to default page on refreshRouterBrowser 不会在刷新时重定向到默认页面
【发布时间】:2021-10-22 14:58:49
【问题描述】:

我有一个登录页面,我希望浏览器默认路由到该页面。我的代码是在第一次运行时这样做的(重定向到 http://localhost:3000/login),但是在我路由到不同的页面并点击刷新后,它不会重定向回登录页面,而是继续它所在的最后一个 URL。我做错了什么?

   return (
    <div>
      <h1>Movies - Subscriptions Web Site</h1>
      <br />
      <Link to="/movies" style={{ textDecoration: "none" }}>
        <input type="button" value="Movies" />{" "}
      </Link>
      <Link to="/subscriptions" style={{ textDecoration: "none" }}>
        <input type="button" value="Subscriptions" />{" "}
      </Link>
      <Link to="/userManagement" style={{ textDecoration: "none" }}>
        <input type="button" value="User Management" />{" "}
      </Link>
      <input type="button" value="Log Out" />

      <Switch>
        <Route exact path="/">
          <Redirect to="/login" />
        </Route>
        <Route path="/login" component={LoginComp} />
        <Route path="/movies" component={MoviesComp} />
        <Route path="/subscriptions" component={SubscriptionsComp} />
        <Route path="/userManagement" component={ManageUsersComp} />
      </Switch>
    </div>
  );

【问题讨论】:

    标签: reactjs react-router react-functional-component


    【解决方案1】:

    删除exact 属性和route 标签

    <Redirect to="/login" />
    

    【讨论】:

    • 我在 之前写了 并且只有这样才有效。实际上这很有意义:) 谢谢!
    【解决方案2】:

    去掉exact和route标签直接写

    <Redirect path="/" to="/login" />
    

    【讨论】:

    • 我在 之前写了 并且只有这样才有效。实际上这很有意义:) 谢谢!
    猜你喜欢
    • 2013-12-26
    • 2021-11-14
    • 2017-07-18
    • 2019-03-09
    • 2020-02-20
    • 1970-01-01
    • 1970-01-01
    • 2012-09-11
    • 2012-07-07
    相关资源
    最近更新 更多