获取地址参数
- URLSearchParams可用优先使用,否则通过切割实现相似功能
getQueryString(propName) {
let url = ''
let searchData = {}
if (!window.location.search) {
const index = window.location.hash.indexOf('?')
if (index !== -1) url = window.location.hash.slice(index)
} else {
url = window.location.search
}
if (!url) return propName ? '' : searchData
if (typeof URLSearchParams === 'function') {
searchData = Object.fromEntries(new URLSearchParams(url))
} else {
decodeURIComponent(url).split('&').forEach((item) => {
const [key, ...value] = item.split('=')
Reflect.set(searchData, key, value.join('='))
})
}
return typeof propName === 'string' && propName ? Reflect.get(searchData, propName) : searchData
},