使用react-router的setRouteLeaveHook的钩子函数

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对象
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。