方法如下:
getUrlstr(name) {
let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
let param = '';
param = window.location.hash.split('?')[1];
if(!param) {
return null
}
let r = param.match(reg);
if (r != null) {
return decodeURIComponent(r[2]);
};
return null;
}
调用:
console.log(getUrlstr("参数名1"))
console.log(getUrlstr("参数名2"))
console.log(getUrlstr("参数名3"))
例子:
地址栏URL为'http://localhost:808/#/?id=785&token=20'
console.log(GetUrlstr("id")) //785
console.log(GetUrlstr("token")) //20
console.log(GetUrlstr("name")) //null
地址栏URL为'http://localhost:808/#/'
console.log(GetUrlstr("id")) //null
console.log(GetUrlstr("token")) //null
console.log(GetUrlstr("name")) //null