美文网首页
react利用 js (点击等)触发控制路由跳转

react利用 js (点击等)触发控制路由跳转

作者: Dr丶net | 来源:发表于2019-05-15 18:49 被阅读0次

    利用 react-router的withRouter进行 js 控制路由跳转

    import React,{Component} from 'react';
    import { withRouter } from 'react-router';
    @withRouter
    export default myComponent extends Component {
        constructor(props){
            super(props)
        }
        routerChange(){
            const { history } = this.props;
            history.push('你要跳转的路由')
        }
        render(){
            return(
                <div>
                    <button 
                        onClick={
                            this.routerChange.bind(this)
                        } 
                    >
                    点我跳转
                    </button>
                </div>
            )
        }
    }
    

    PS:上述代码使用了 babel 装饰器方法,相关配置方法请点击下方链接

    creat-react-app Ver2.0+配置装饰器语法

    相关文章

      网友评论

          本文标题:react利用 js (点击等)触发控制路由跳转

          本文链接:https://www.haomeiwen.com/subject/tipmaqtx.html