项目需要,之前的显性传值改为隐性传值:
//显性传值
router.push( pathname:`/bondApproval?id=${id}&type=haveTodo&title=标题`);
//隐性传值
const path = {
pathname: '/approval',
state: {
type: 'haveTodo',
id,
title:'标题'
}
}
router.push(path);
获取方式:
//显性传值获取方式
const getParamValue = (target) => {
const search = window.location.search.substring(1);
const params = search.split('&');
for (let i = 0; i < params.length; i++) {
const param = params[i].split('=');
if (param[0] === target) {
return param[1];
}
}
return false;
}
const id = getParamValue ('id')
//隐性传值获取方式
this.props.location.state
本文介绍了一种在项目中改进参数传递的方法,从显性传值转换为隐性传值,对比了两种方式的代码实现,并提供了获取传值的示例。这对于提升前端应用的用户体验和代码维护性具有重要意义。

4347

被折叠的 条评论
为什么被折叠?



