今天在使用react-router setRouteLeaveHook的鉤子函數,不知道怎麼實驗就是成功。最後功夫不負有心人,終於讓我找到使用setRouteLeaveHook的方法了javascript
一、我在網絡上找到關於如何使用的方法,java
componentDidMount(){ this.props.router.setRouteLeaveHook( this.props.route, this.routerWillLeave ) } routerWillLeave=(nextLocation)=>{ return '確認要離開?'; }
可是,歐額,要報錯react
沒法找到props裏面的router,網絡
廢了很大的勁,終於找到問題所在,react-router
緣由:函數
缺乏react-router的 withRouter方法加工一下組件,因此找不到props裏面的routerthis
解決辦法:spa
一、在組件頭部引入react-router模塊,要導入withRoutercomponent
import { withRouter } from 'react-router'
二、在導出組件的時候,經過withRouter方法對組件進行加工router
export default withRouter(Login)
代碼編譯好以後,從新進入到頁面,路由跳轉就會出現提示