react-router4.0有没有类似vue里的this.$router.push({path:'/Home'})

如题,react-router4.0如何在做完某个逻辑时直接切换路由?

阅读 5.1k
3 个回答

this.props.history.push('/Home')

解决了,需要把组件使用withRouter包一层,具体代码实例

import React, {Component} from 'react'
import {withRouter} from 'react-router-dom'

class Foo extends Component {
  constructor(props) {
    super(props)

    this.goHome = this.goHome.bind(this)
  }

  goHome() {
    this.props.history.push('/')
  }

  render() {
    <div className="foo">
      <button onClick={this.goHome} />
    </div>
  }
}

export default withRouter(Foo)

链接:链接描述

this.props.history.push({pathname:"/Home"}
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题