将带参数的url地址中的参数提取组合成对象
const getUrlParams = (search) => {
const queryString = (search && search.split('?')[1]) || location.search.split('?')[1] || ''
const queryList = queryString.split('&')
let result = {}
queryString && queryList.map((item) => {
let keyValue = item.split('=')
result[keyValue[0]] = decodeURIComponent(keyValue[1])
})
return result
}
将参数拼成对象,作为参数传入,返回值是拼接后的url参数,不带问号 ' ? '
const getParamStrFromObj = (data = {}, paramProxy = '') => Object.keys(data)
.map(key => {
if (paramProxy) {
return `${paramProxy}[${key}]=${encodeURIComponent(data[key])}`
}
return `${key}=${encodeURIComponent(data[key])}`
})
.join('&')
网友评论