vue分解Url参数配置文件

原创
小哥 3年前 (2022-11-05) 阅读数 108 #大杂烩

/**

  • 解析url参数
    */
    export function parseQuery() {
    const res = {};

const query = (location.href.split("?")[1] || "")
.trim()
.replace(/^(\?|#|&)/, "");

if (!query) {
return res;
}

query.split("&").forEach(param => {
const parts = param.replace(/+/g, " ").split("=");
const key = decodeURIComponent(parts.shift());
const val = parts.length > 0 ? decodeURIComponent(parts.join("=")) : null;

if (res[key] === undefined) {
res[key] = val;
} else if (Array.isArray(res[key])) {
res[key].push(val);
} else {
res[key] = [res[key], val];
}
});

return res;
}

‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’‘’

import { parseQuery } from @/utils

let { token } = parseQuery();

版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门