编程式导航

react-router-dom中通过history对象中的push/replace/go等方法实现编程式导航功能

this.props.history.push(path)

this.props.history.push({

pathname:’’,

search:’’,

state:{}

})

# 路由监听

App.jsx组件中可以添加路由监听

constructor(props) {

    super(props);

    this.props.history.listen(route=>console.log(route))

}

注:一定要在当前组件中的props中能得到路由对象才可以

路由监听

注:默认App.jsx组件中没有this.props.history方法,需要通过withRouter高阶组件来进行包裹,才能得到。

右侧打赏一下 代码改变世界一块二块也是爱
原文地址:https://www.cnblogs.com/ht955/p/14830979.html