react-router使用history.push路由跳转出错问题
- 路由页面
http://localhost:8888/detail/5aae1cc8f5dfc27d7ad98909
- 路由形式
<Route path="/detail/:id" component = {TopicDetail} key = "detail" />
<Route path="/user/login" component = {UserLogin} key = 'login' />
class TopicDetail extends React.Component{
static contextTypes = {
router:PropTypes.object,
}
constructor(props){
super(props)
this.goToLogin = this.goToLogin.bind(this)
}
/*-------登入并回复-------*/
goToLogin() {
this.context.router.history.push('user/login')
}
render() {
return (
<div>
<section className={classes.notLoginButton}>
<Button raised color="inherit" onClick={this.goToLogin}>
登入并回复
</Button>
</section>
</div>
)
}
}
- 问题:单击按钮调用 this.context.router.history.push('user/login'),为什么路由从
http://localhost:8888/detail/5aae1cc8f5dfc27d7ad98909
跳转到http://localhost:8888/detail/user/login
而不是http://localhost:8888/user/login
最好给下代码Demo,这样没法解决