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對象