javascript - 关于react-router leave钩子的问题
阿神
阿神 2017-04-11 12:16:12
0
1
215

我想让用户在当前路由跳转到别的路由前有一个confirm提示,但是我所有的路由都是用配置文件配置的,不是用react组件写的
代码:

        childRoutes: [
            {
                path: 'manage/cat',
                component: CatManager
            }, {
                path: 'manage/sku',
                indexRoute: {
                    component: SkuManager
                },
                childRoutes: [
                    {
                        path: 'add',
                        component: SkuCreate,
                        routerWillLeave: (nextLocation) => {
                            console.log('react router 路由跳转=======>',nextLocation);
                            return false;
                        }
                    }
                ]
            }

我想让用户在离开/manage/sku/add路由时执行一些动作,该怎么写?现在这样写并不执行啊

阿神
阿神

闭关修行中......

reply all(1)
巴扎黑

应该是onLeave事件吧

routerWillLeave 是高阶组件容器withRouter重写的方法。你如果要用的话应该在组件内部使用,如下

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 <p>go</p>
  }
})

export default withRouter(Demo)
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!