[英]How to decode the URL when navigating one route to another roue in angular 5?
I want to navigate a page from one route another route with query parameters. 我想使用查询参数从一个路由导航到另一个路由页面。
var name = "engine & machinery";
this.router.navigateByUrl('dashboard?isopen='+true+'&name='+name);
From this, route navigate to dashboard page but query parameters not coming properly. 由此,导航到仪表板页面,但查询参数不正确。
Url comes like, 网址来了,
http://localhost:4200/dashboard?isopen=true&name=engine%20&%20machinery=
Getting query parameters by the following code, 通过以下代码获取查询参数,
console.log("testt", this.route.snapshot.queryParamMap.get('name'));
its return only the "engine"
. 它只返回
"engine"
。
So, anyone help me for decoding the url and how to get the parameter value? 因此,有人可以帮助我解码url以及如何获取参数值吗?
Thanks in advance 提前致谢
Use encodeURIComponent
to encode the URL with special characters like these. 使用
encodeURIComponent
使用此类特殊字符对URL进行编码。
var name = encodeURIComponent("engine & machinery");
while creating the URL. 在创建URL时。
And in the receiving end, decode it using the decodeURIComponent
然后在接收端,使用
decodeURIComponent
对其进行解码
Something like this: 像这样:
const encodedName = this.route.snapshot.queryParamMap.get('name')
const decodedName = decodeURIComponent(encodedName);
// This would yield `engine & machinery`
Here's a Sample StackBlitz for your ref. 这是供您参考的示例StackBlitz 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.