通过对象方式获取到链接上的参数
html
<template>
<div>
卡号{{cardNo}}
</div>
</template>
script
export default{
data(){
return{
cardNo:"",
obj:{}
}
},
mounted() {
let url = window.location.href;
this.obj = this.getQueryObject(url);
this.cardNo = this.obj.cardNo;
console.log(this.obj)
},
methods: {
//转码
urlencode(str) {
str = (str + "").toString();
return encodeURIComponent(str)
.replace(/!/g, "%21")
.replace(/'/g, "%27")
.replace(/\(/g, "%28")
.replace(/\)/g, "%29")
.replace(/\*/g, "%2A")
.replace(/%20/g, "+");
},
getQueryObject(url) {
url = url == null ? window.location.href : url;
let ty = url.substring(url.lastIndexOf("?") + 1);
const cc = ty + "&type=1";
const search = cc.split("#")[0];
const obj = {};
const reg = /([^?&=]+)=([^?&=]*)/g;
search.replace(reg, (rs, $1, $2) => {
const name = decodeURIComponent($1);
// let val = decodeURIComponent($2);
let val = $2;
console.log(val);
val = String(val);
obj[name] = val;
return rs;
});
return obj;
},
},
}