定义两个变量:
returnArr:{},
tempArr:''
getChannelCode() {
/**
* tempArr 是一个字符串数组 格式是["key=value", "key=value", ...]
*/
this.tempArr = window.location.href.split("?")[1];
if (this.tempArr != undefined) {
/**
* returnArr 是要返回出去的数据对象 格式是 { key: value, key: value, ... }
*/
// console.log(this.tempArr);
if (this.tempArr.indexOf("&") > -1) {
//如果地址中包含有&,则通过&去分割参数并且取对应的参数
let newArr = this.tempArr.split("&") ? this.tempArr.split("&") : [];
newArr.forEach((element) => {
this.returnArr[element.split("=")[0]] = element.split("=")[1];
});
} else {
//如果没有则直接取?后面的参数,通过=分割
this.returnArr[this.tempArr.split("=")[0]] =
this.tempArr.split("=")[1];
}
}
},
获取网址链接参数方法
于 2022-08-24 16:14:55 首次发布