在src-utils目录下新建一个文件,用于暴露获取参数函数
src
utils
helper
定义文件
export function getQueryString(url) {
url = url == null ? window.location.href : url;
const search = url.substring(url.lastIndexOf('?') + 1);
const obj = {};
const reg = /([^?&=]+)=([^?&=]*)/g;
search.replace(reg, (rs, $1, $2) => {
const name = decodeURIComponent($1);
let val = decodeURIComponent($2);
val = String(val);
obj[name] = val;
return rs;
});
return obj;
}
在需要用到的页面,引入文件
import { getQueryString } from 'utils/helper';
created() {
let query = new getQueryString();
console.log(query, 'query');
},