在URL中,当在Angular中路由时使用`queryParams`时,`%`被'%25'替换

问题描述 投票:-1回答:1

我想在使用角色路由时使用queryParams导航到URL。

<a routerLink='/master' [queryParams]="{query:'%US',mode:'text'}"><li (click)="search()">Search</li></a>

我想要导航的URL是:

http://localhost:4200/master?query=%US&mode=text

但当我点击搜索时,它导航我:

http://localhost:4200/master?query=%25US&mode=text

我不知道为什么在25符号后附加%。任何人都可以告诉我一个更清洁的方式正确导航。

javascript angular typescript routing angular4-router
1个回答
1
投票

在URL中,百分号具有特殊含义,用于编码特殊字符。例如,=被编码为%3D。

url中不允许使用某些特殊字符。如果你想在url中使用它们,你必须使用encodeURIComponent javascript函数对它们进行编码。 %25实际上是%字符的编码版本。浏览器在这里对它们进行编码。

尝试从url获取queryParams时,可以使用decodeURIComponent解码它们。

有关更多信息,请查看:https://support.microsoft.com/en-in/help/969869/certain-special-characters-are-not-allowed-in-the-url-entered-into-the

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent

© www.soinside.com 2019 - 2024. All rights reserved.