【问题标题】:Encoding Uri using Link of react router dom not working使用反应路由器 dom 的链接编码 Uri 不起作用
【发布时间】:2018-01-30 13:49:36
【问题描述】:

我想使用 react-router-dom 的链接标签对 uri 进行编码,但是当我使用 javascript encodeURIComponent 对 uri 进行编码时,它不会在地址栏(url 栏)中显示编码的 uri,尽管我在悬停时看到 uri 编码。

<Link key={i}  to={encodeURIComponent(item.url)}> {item.text}</Link>  

我是遗漏了什么还是 uri 在 react router dom 中被解码。

【问题讨论】:

  • 尝试像这样修复&lt;Link key={i} to={encodeURIComponent(item.url)}}&gt; {item.text}&lt;/Link&gt;
  • 安德鲁 我用的一样...我编辑了我的问题...谢谢

标签: javascript reactjs react-router-dom


【解决方案1】:

react router dom 中的链接在将 uri 推送到历史记录时对其进行解码。
任何人都可以在 github 和相关问题中阅读此问题 路径在 createLocation #505 中解码
https://github.com/ReactTraining/history/issues/505

我通过对 uri 进行双重编码来解决此问题,以便在推送历史记录时对其进行一次解码。
encodeURIComponent(encodeURIComponent(item.url))
到目前为止,我还没有发现任何副作用....希望这可以帮助任何面临这个问题的人。

【讨论】:

  • 道歉我确实发现了一个副作用,如果有人右键单击并尝试复制链接地址,它将被编码两次......我不知道如何解决这个问题。
  • 这个问题你解决了吗?我有一个搜索栏,我需要使用history.push() 将搜索文本推送到 URL,但到目前为止我无法对其进行编码。它总是在 URL 中解码。
猜你喜欢
  • 2021-09-11
  • 1970-01-01
  • 2017-03-06
  • 2021-12-21
  • 2020-12-25
  • 2021-09-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多