使用react-router的链接编码 Uri 不工作

IT技术 javascript reactjs react-router-dom
2021-04-30 17:11:48

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

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

我是否缺少任何东西或uri在react router dom中被解码。

1个回答

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

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