从路径中取出传过来的参数

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26983555/article/details/79369792

出现的场景:
从一个页面跳转到另一个页面的时候,有时候我们需要传过去id或者具体的value等参数,可以使用添加到路径上的方法。
例如: &id=*放在路径的后面的样子
然后使用:


export const getQueryString = function getQueryString(search,name) {
var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
var r = search.substr(1).match(reg);
if (r != null) {
return unescape(r[2]);
}
return null;
获取传出去的数值

传出去的页面可以这样写:


<Link to={{
pathname: '/orderCenter/list/details',
search: `?id=${record.id}`,
// state: {
// id: record.id,
// },
}} >
<span onClick={e => this.details(e, record.id)} key={record.id}>详情</span>
</Link>
(这里在实践的过程中发现,如果使用下面的方式传过去id,即id是不会在路径上显示出来的
// state: {
// id: record.id,
// }, 那么在取值的那个页面刷新之后,id的值会变成undefined)

需要的页面就可以:

id: getQueryString( this.props.location.search, 'ID'),

解释:this.props.location.search就是从路径上取出我们传过去的值,然后赋给id

猜你喜欢

转载自blog.csdn.net/qq_26983555/article/details/79369792