簡體   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