记录一下遇到的问题,在网上查了好多,都没有找到答案
希望能帮助到你!
1.当重定向到某个服务的地址如下,我们需求取后面的参数 id1 和 id2
例如:https://aa.bb.cn/Server/?id1=46&id2=13&id3=14
2.在index.vue 的添加如下函数,这样跳转页面后,就能获取的参数了。
代码1:
mounted() {
var that=this;
this.getid();//获取URL传参
},
代码2:
methods: {
getid(){
let obj = {};
// 获取url参数部分 大概长这样 ?id=213123&a=b
let url = window.location.search;
let reg = /[?&][^?&]+=[^?&]+/g;
// 用正则匹配成数组 大概长这样 [?id=213123, &a=b]
let arr = url.match(reg);
if (arr) {
arr.forEach((item) => {
// 把字符串?id=123 转为数组 [id, 123]
let tempArr = item.substring(1).split('=');
console.log(tempArr);
// decodeURIComponent()可对encodeURIComponent()函数编码的URI进行解码。
let key = decodeURIComponent(tempArr[0]);
console.log(key);
let val = decodeURIComponent(tempArr[1]);
console.log(val);
// 把键值对添加到obj中
obj[key] = val;
})
}
//console.log(obj.id1);
this.username=obj.id2;
this.ava=obj.id3;
}
}
3.总结:如果能帮到你,麻烦点赞支持!谢谢