刷新页面后保留url个查询

keep url query after refreshing the page

在我的应用中,如果用户转到

http://localhost:3000/#/auth

登录页面将默认呈现。

当用户进入注册页面时,url 是

http://localhost:3000/#/auth?form=signup

但是,每当我刷新页面时,url 会自动返回到

http://localhost:3000/#/auth

用户现在可以看到登录页面。

有没有办法在刷新后保留 url 查询?换句话说,如果用户在注册页面上并刷新,我想呈现注册页面而不是登录页面。

我正在使用 react-router@3 并使用 hashHistory。这是路线

<Route path="/auth" component={Auth} />

我明白我的问题是什么了。刷新期间未清除查询字符串。我已经根据用户是否在顶级组件中登录设置了重定向。

  _redirect(isLoggedIn) {
    let route = this.props.location.pathname.slice(1);

    if (isLoggedIn) {
      this.props.router.replace('/home');
    } else if (!isLoggedIn && !/auth/.test(route)) {
      this.props.router.replace('/auth');
    }
  } 

我的问题是 else if 条件,我没有检查路由是否包含 auth。因此,如果用户未登录,此函数将重定向到 /auth 并删除查询字符串。

  _redirect(isLoggedIn) {
    let route = this.props.location.pathname.slice(1);

    if (isLoggedIn && /auth/.test(route)) {
      this.props.router.replace('/home');
    } else if (!isLoggedIn && !/auth/.test(route)) { <== updated this line
      this.props.router.replace('/auth');
    }
  }