繁体   English   中英

在角度5中将一条路线导航到另一条路线时,如何解码URL?

[英]How to decode the URL when navigating one route to another roue in angular 5?

我想使用查询参数从一个路由导航到另一个路由页面。

var name = "engine & machinery";

this.router.navigateByUrl('dashboard?isopen='+true+'&name='+name);

由此,导航到仪表板页面,但查询参数不正确。

网址来了,

http://localhost:4200/dashboard?isopen=true&name=engine%20&%20machinery=

通过以下代码获取查询参数,

console.log("testt", this.route.snapshot.queryParamMap.get('name'));

它只返回"engine"

因此,有人可以帮助我解码url以及如何获取参数值吗?

提前致谢

使用encodeURIComponent使用此类特殊字符对URL进行编码。

var name = encodeURIComponent("engine & machinery");

在创建URL时。

然后在接收端,使用decodeURIComponent对其进行解码

像这样:

const encodedName = this.route.snapshot.queryParamMap.get('name')
const decodedName = decodeURIComponent(encodedName);
// This would yield `engine & machinery`

这是供您参考的示例StackBlitz

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM