【问题标题】:How to redirect to a brand new route with history push?如何通过历史推送重定向到全新的路线?
【发布时间】:2020-08-30 21:29:43
【问题描述】:

我正在使用props.history.push('/something') 从路线'/test'

但后来我被重定向到'/test/something' 而不是'/something' 我怎样才能做到这一点?

【问题讨论】:

  • 你使用的是props.history.push('/something')还是props.history.push('something')

标签: javascript reactjs react-router history


【解决方案1】:

如果你使用hashRouter,那么你可以简单地这样写。

this.props.history.push('#/something')

【讨论】:

    【解决方案2】:

    作为一种解决方法,我认为如果您创建从一条路径到另一条路径的重定向,那么它将起作用。

    尝试如下:

    <Route exact path="/test/something">
        <Redirect to="/something" />
    </Route>
    

    所以这样从/test/something 会重定向到/something

    我希望这会有所帮助!

    【讨论】:

    • 这是唯一的方法吗?它看起来有点老套。
    • @AlexK 对于这种情况,使用history.push() 我不知道任何其他解决方案。如果它适合你,你能测试一下吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-01
    • 2020-05-29
    • 2020-03-14
    • 1970-01-01
    • 2011-06-27
    • 2012-10-22
    • 2013-06-02
    相关资源
    最近更新 更多