vue 重定向到其他服务,怎么获取url的参数

记录一下遇到的问题,在网上查了好多,都没有找到答案

希望能帮助到你!

1.当重定向到某个服务的地址如下,我们需求取后面的参数 id1 和 id2

例如:https://aa.bb.cn/Server/?id1=46&id2=13&id3=14

2.在index.vue 的添加如下函数,这样跳转页面后,就能获取的参数了。

  代码1mounted() {
    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.总结:如果能帮到你,麻烦点赞支持!谢谢

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值