问题描述
我想使用 react-router-dom 的 Link 标签对 uri 进行编码,但是当我使用 javascript encodeURIComponent 对 uri 进行编码时,它不会在地址栏(网址栏)中显示编码的 uri,尽管我在悬停时看到 uri 已编码.
I want to encode uri, using Link tag of react-router-dom but when I use javascript encodeURIComponent to encode uri it does not show encoded uri in address bar(url bar) though i see uri encoded when i hover.
<Link key={i} to={encodeURIComponent(item.url)}> {item.text}</Link>
我是否遗漏了什么或 uri 在 react router dom 中被解码.
Am I mising anything or uri is decoded inside react router dom.
推荐答案
react router dom 中的 Link 解码 uri,同时将其推送到历史记录.
任何人都可以在 github 和相关问题中阅读此问题路径在 createLocation #505 中解码
https://github.com/ReactTraining/history/issues/505
The Link in react router dom decodes the uri while pushing it to history.
Anybody can read this issue in github and related issuesPath is decoded in createLocation #505
https://github.com/ReactTraining/history/issues/505
我通过对 uri 进行双重编码来解决此问题,以便在推送历史记录时对其进行一次解码.
encodeURIComponent(encodeURIComponent(item.url))
到目前为止,我还没有发现任何副作用......希望这可以帮助任何面临这个问题的人.
I did workaround of this issue by double encoding the uri so that it is decoded once while pushing in history.
encodeURIComponent(encodeURIComponent(item.url))
till now I have not found any side effects of this....hope this helps anybody facing this issue.
这篇关于使用反应路由器的链接编码 Uri 不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!