在开发过程中我们经常遇到url中携带参数,如果能把参数储存的一个对象中,将会使我们的开发效率提高很多
通过 location.search 我们可以拿到url中的参数
下面写个方法处理一下
//
const getQueryParams = () => {
const parmas = {}
// 获取url的参数
const query = location.search
const reg = /[?&][^?&]+=[^?&]/g
const found = query.match(reg)
if (found) {
found.forEach((e) => {
const item = e.substring(1).split('=')
parmas[item[0]] = item[1]
})
return parmas
}
}
console.log(getQueryParams())