[英]Turn URL Object into URl string
So I have a stringified object in my localStorage,所以我的 localStorage 中有一个字符串化的对象,
const stringifiedURLObject = fromLocalStorage
I tried to JSON.parse that string to normal Url Object.我尝试将该字符串 JSON.parse 解析为普通的 Url 对象。 and I want to convert that object into Url string,我想将该对象转换为 Url 字符串,
// i want to turn this
{
pathname: 'user/[id]'
query: {
mode: 'active'
id: 'someID'
}
}
// to this
const normalPathURL = `/user/xxx?mode=active`
Then pass the string to redirect query然后传递字符串以重定向查询
{
pathname: '/register',
query: {
redirect: normalPathURL,
},
}
How can i do that?我怎样才能做到这一点?
You can do this easily with javascript string templates:您可以使用 javascript 字符串模板轻松完成此操作:
const parsedObject = {
pathname: 'user/[id]'
query: {
mode: 'active'
id: 'someID'
}
}
// to this
// parsedObject.pathname.split('/')[0] extracts first part of pathname
const normalPathURL = `/${parsedObject.pathname.split('/')[0]}/${parsedObject.query.id}?mode=${parsedObject.query.mode}`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.