HashRouter Cannot read property 'push' of undefined

场景
在子组件使用history跳转时失败

this.props.history.push(a);

Cannot read property ‘push’ of undefined

原因

Router 组件的后代里头才有可能有 this.props.history
而我跳转方法所在的组件是后代组件的子组件,所以undefined了

解决
将history对象传到要用的子组件中

<JsPageBottomNavigation history={this.props.history}></JsPageBottomNavigation>

然后就可以使用了

猜你喜欢

转载自blog.csdn.net/weixin_39168678/article/details/80608902