代码之家  ›  专栏  ›  技术社区  ›  Sophia Price

响应路由器未在重定向时更新URL

  •  0
  • Sophia Price  · 技术社区  · 7 年前

    我有一个应用程序正在使用 .我有一个匹配的页面,除非您登录,否则您无法访问。当用户未登录并尝试转到 /match 它们被重定向到 /login .我的问题是当用户被重定向到 /登录 该URL保持为/match。这在我的代码中的其他地方引起了问题。当用户被重定向到 /登录 是吗?我正在从app.js中的开关重定向页面。下面是我的代码的一个示例:

    <Route
        path='/match'
        component= {
            () => {
                if (this.state.auth) return <HomePage />;
                else return <LoginPage />;
            }
        }
    />
    

    TL;医生

    如何获取要更改为的URL /登录 如果用户在尝试访问时未登录 /匹配 .

    3 回复  |  直到 7 年前
        1
  •  1
  •   Jimi Pajala Levi D.    7 年前

    <Redirect />

    <Route
      path='/match'
      render={
        () => {
          if(this.state.auth){ return <HomePage /> }
          else { return <Redirect to="/login" /> }
        }
      }
    />
    <Route match="login" component={Login} />
    

    props.history.push('/login')

        2
  •  2
  •   Tholle    7 年前

    /login Redirect /match

    <Route
      path='/match'
      render={
        () => {
          if (this.state.auth) return <HomePage />;
          else return <Redirect to='/login'/>;
        }
      }
    />
    <Route
      path='/login'
      render={LoginPage}
    />
    
        3
  •  0
  •   Moonjsit    7 年前