###js获取url中的某个参数
方法一:正则匹配
getQuery(key) {
try {
const href = window.location.href
const reg = new RegExp(`${key}=[^&|#|\/]*`)
const res = href.match(reg)
return res ? href.match(reg)[0].split('=')[1] : ''
} catch (error) {
console.error(error)
}
}
方法二:循环判断
getQuery(key) {
let query = window.location.href.split("?")[1] || "";
let vars = query.split("&");
for (let i = 0; i < vars.length; i++) {
let pair = vars[i].split("=");
if (pair[0] == key) {
return pair[1];
}
}
return false;
},