米琪卡哇伊
2018-10-16 13:15:29
我想讓用戶在當(dāng)前路由跳轉(zhuǎn)到別的路由前有一個(gè)confirm提示,但是我所有的路由都是用配置文件配置的,不是用react組件寫(xiě)的代碼: childRoutes: [ { path: 'manage/cat', component: CatManager }, { path: 'manage/sku', indexRoute: { component: SkuManager }, childRoutes: [ { path: 'add', component: SkuCreate, routerWillLeave: (nextLocation) => { console.log('react router 路由跳轉(zhuǎn)=======>',nextLocation); return false; } } ] }我想讓用戶在離開(kāi)/manage/sku/add路由時(shí)執(zhí)行一些動(dòng)作,該怎么寫(xiě)?現(xiàn)在這樣寫(xiě)并不執(zhí)行啊
1 回答
回首憶惘然
TA貢獻(xiàn)1847條經(jīng)驗(yàn) 獲得超11個(gè)贊
應(yīng)該是onLeave事件吧
routerWillLeave 是高階組件容器withRouter重寫(xiě)的方法。你如果要用的話應(yīng)該在組件內(nèi)部使用,如下
import React from 'react'
import { withRouter } from 'react-router'
const Demo = React.createClass({
componentDidMount() {
this.props.router.setRouteLeaveHook(this.props.route, () => {
return 'funk away'
})
},
render() {
return <div>go</div>
}
})
export default withRouter(Demo)
添加回答
舉報(bào)
0/150
提交
取消
