Vue获取地址栏?后携带的所有参数,并将其转换为Object,作为参数传输给后端
<template>
<div class="preview">预览</div>
</template>
<script>
export default {
data() {
return {
objectParams:{},
}
},
created() {
this.getRequest()
},
methods: {
getRequest() {
let url = decodeURI(window.location.search || window.location.hash)
let strs
if (url.indexOf("?") != -1){
if(window.location.hash) {
strs = url.split("?")[1].toString().split("&")
}else{
strs = url.substr(1).split("&")
}
for (let i = 0; i < strs.length; i++) {
this.objectParams[strs[i].split("=")[0]] = strs[i].split("=")[1]
}
}
if(Object.keys(this.objectParams).length > 0){
console.log(this.objectParams);
}
}
}
}
</script>