这是我的路由配置代码:
<Switch>
<Route exact path='/(home)?' component={TodoListHomePage} />
<Route exact path='/profile/:userId' component={TodoListProfilePage} />
<Route path='/login' component={SignUpAndLogin} />
</Switch>
一切正常,但参数路径很奇怪,第一次单击它工作正常,路径例如
第二次单击它会连接个人资料,如下所示:
http://localhost:4000/Profile/Profile/597c1f43a87ca40d38f79a68
例如
http://localhost:4000//Profile/Profile/Profile/Profile/597c1f43a87ca40d38f79a68
这是我在单击个人资料链接时调用的函数:
handleProfileClick = (e, {name}) => {
this.setState({ activeItem: name });
this.props.history.push(name.concat('/'.concat(this.props.viewer._id.toString())));
}
最佳答案
您正在使用相对路径。只需在连接之前添加一个 /
即可。
handleProfileClick = (e, {name}) => {
this.setState({ activeItem: name });
this.props.history.push("/profile/" + this.props.viewer._id.toString());
}
关于javascript - React Router Dom 参数无法正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45658203/