如何解决有什么方法可以删除查询字符串上唯一键参数处的等号? 问题
问题
我使用了无值查询字符串参数,但是如果我移到另一个页面(路由),则url会更改,并为无值参数添加了新的等号。该页面由react js单页面应用程序编码。
ex) http://test.com:3000/abc?a=1&b=2&c&d=4-> http://test.com:3000/abc?a=1&b=2&c=&d=4
即使我移到另一页,有没有办法去除等号?
解决方法
如果您使用URLSearchParams
将某些对象转换为查询字符串,然后直接传递给Link
/ Redirect
或其他任何方法。你可能想在通过之前做这样的事情,
const queryObj = {
something: 'blah',confirm: '',else: 'features'
}
const query = new URLSearchParams(queryObj)
console.log(query.toString())
console.log(query.toString().replace(/=$|=(?=&)/g,'')) // with NoEmptyEqualSign
这里有query
更改(保持pathname
不变)sandbox link的一些示例。
1:使用Link
<Link to="?confirm">No equal Sign</Link>
<Link to="?confirm=">Equal Sign with no value</Link>
<Link to="?confirm=launch">Equal Sign with value</Link>
2:使用Redirect
没有等号
<Redirect to={{ pathname: history.location.pathname,search: "?confirm" }} />
没有值的等号
<Redirect to={{ pathname: history.location.pathname,search: "?confirm=" }} />
等号与值
<Redirect to={{ pathname: history.location.pathname,search: "?confirm=launch" }} />
添加了额外的pathname
和search
,以保持pathname
不变
3:使用useHistory
没有等号
history.push({ search: "?confirm" })
没有值的等号
history.push({ search: "?confirm=" })
等号与值
history.push({ search: "?confirm=launch" })
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。