使用react-router的setRouteLeaveHook

2018-11-09  本文已影响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对象
上一篇下一篇

猜你喜欢

热点阅读