美文网首页
使用react-router的setRouteLeaveHook

使用react-router的setRouteLeaveHook

作者: 里渭水塘十强选手 | 来源:发表于2018-11-09 17:17 被阅读0次

    setRouteLeaveHook是react-router离开页面的钩子函数
    使用方法如下:

     componentDidMount() {
        this.props.router.setRouteLeaveHook(this.props.route, (nextLocation) => {
          if (nextLocation.pathname.indexOf('wxConcernSuccess') >= 0) {
            console.log('我离开了')
          }
        })
      }
    

    注意 在导出组件时要用withRouter包裹

    import { withRouter } from 'dva/router'
    ...
    export default withRouter(Index)
    

    PS:nextLocation是一个对象包括了pathname、query等参数

    nextLocation对象

    相关文章

      网友评论

          本文标题:使用react-router的setRouteLeaveHook

          本文链接:https://www.haomeiwen.com/subject/tymoxqtx.html