代码之家  ›  专栏  ›  技术社区  ›  learner

ConnectedRouter-使用浏览器后退功能以编程方式推送路由

  •  0
  • learner  · 技术社区  · 7 年前

    我正在使用react router redux(5.0.0-alpha.6)的ConnectedRouter。这是示例代码

    import {Route, Switch} from 'react-router';
    import {ConnectedRouter} from 'react-router-redux';
    
    ReactDOM.render(
        <Provider store={app.store}>
            <ConnectedRouter history={app.history}>
                <div>
                    <Switch>
                        <Route exact path="/about" component={About} />
                        <Route exact path="/:animalName?" component={Animalpage} />
                    </Switch>
                </div>
            </ConnectedRouter>
        </Provider>, document.getElementById('root')
    );
    

    我有一个要求,如果我在“/”上着陆,那么它应该路由到默认动物(比如猫)。

    我使用react router redux的推送功能来实现这一点

    dispatch(push("/Cat"));
    

    有没有办法达到我的要求,去默认路线,让浏览器返回按钮工作?

    1 回复  |  直到 7 年前
        1
  •  2
  •   bennygenel    7 年前

    你应该使用 replace() 行动而不是 push() here

    history methods of the same name
    参考定义如下:

    • -将新位置推到历史记录,成为当前位置。
    • 代替
    • -在历史上向后或向前移动相对数量的位置。
    • -向前移动一个位置。相当于go(1)
    • 戈巴克 -向后移动一个位置。相当于go(-1)