React路由跳转Link的使用

Link标签的三种参数传递方式

1. params参数传递(使用target=“_blank”,参数传递成功,可以在新页面获取到值)

参数传递

// 参数传递
<Link target="_blank" to={`/link/devCode=${getUserInfo().devcode}/fileKey=${record.key}`}></Link>

注册路由,需要声明参数

// 注册路由,需要声明参数
{
    path: "/link/devCode=:devCode/fileKey=:fileKey",
    exact: true,
    component: SuperLink
}

参数接收

// 参数接收
const {devCode, fileKey} = this.props.match.params;

2. search参数传递(使用target=“_blank”,参数传递成功,可以在新页面获取到值)

参数传递

// 参数传递
<Link target="_blank" to={`/link?devCode=${getUserInfo().devcode}&fileKey=${record.key}`}></Link>

注册路由,无需声明参数

// 注册路由,无需声明参数
{
    path: "/link",
    exact: true,
    component: SuperLink
}

参数接收

// 参数接收
const{search} = this.props.location;
// console.log(this.props.location.search);// ?devCode=5ac43b0dbe0e0aab455c183a7ca91fa5&fileKey=8895249
const {devCode, fileKey} = qs.parse(search.slice(1));
// console.log(qs.parse(search.slice(1))); // {devCode: '5ac43b0dbe0e0aab455c183a7ca91fa5', fileKey: '8895249'}

3. state参数传递(使用target=“_blank”,参数传递失败,无法在新页面获取到值;不使用target=“_blank”,参数传递成功)

参数传递

// 参数传递
<Link to={
	{
		pathname: '/link',
		state: {
			devCode: getUserInfo().devcode,
			fileKey: record.key
		}
	}
}></Link>

注册路由,无需声明参数

// 注册路由,无需声明参数
{
    path: "/link",
    exact: true,
    component: SuperLink
}

参数接收

// 参数接收
const {devCode, fileKey} = this.props.location.state || {};
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值