我正在尝试在 URL 中传递参数,但读取时遇到问题。我正在使用反应路由器 v4。
URL: http://localhost:3000/reset?token=123 http://localhost:3000/reset?token=123我试着这样读:
<Route
path="/reset?token=:token"
component={Reset}/>
但这会打印空对象console.log(this.props.match.params);
。奇怪的是,我尝试将问号更改为其他字符,看起来它解决了我的问题,但无论如何我想保留问号。
URL: http://localhost:3000/reset_token=123 http://localhost:3000/reset_token=123
<Route
path="/reset_token=:token"
component={Reset}/>
这已经有效了console.log(this.props.match.params);
。知道如何使问号也正确工作吗?对我来说真正重要的是继续使用react-router而不使用任何外部库。
干杯,
丹尼尔
我是如何解决这个问题的。
http://localhost:3000?token=mypetismissing
export default function ({ location }) {
const urlParams = new URLSearchParams(location.search);
const token = urlParams.get('token');
console.log(myParams)
return (
<div>
{token}
</div>
)
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)