使用this.props.history.push( )方法报错
问题描述:
如下图所示:整体是一个父组件:BCustomerList;父组件里面引入了一个列表,是子组件:BCustomerListChild;现点击列表调转到一个详情的组件:BCustomerDetail;使用方法为this.props.history.push( ),结果点击列表的时候报错:Uncaught TypeError: Cannot read property ‘push’ of undefined
解决办法:
方法一:
在子组件BCustomerListChild中:
引入:import { withRouter } from ‘react-router’
export时要使用withRouter:export default withRouter(BCustomerListChild)
方法二
在父组件BCustomerList中:
使用子组件的时候加入:history={this.props.history}
如代码所示:
<BCustomerListChild history={this.props.history}></BCustomerListChild>
再次点击列表跳转到详情组件BCustomerDetail的时候,即可正常跳转,happy~~
φ(゜▽゜*)♪