window.location.href跳转页面并传值及接收值
传值通过?在url上拼接值,进行传递。下方分别是单参数传递和多参数传递:
window.location.href = "/static/test.html?type=" + this.selected window.location.href = "/static/test.html?type=" + this.selected + &value= + val;
接收值:
//获取地址栏参数,name:参数名称
function getUrlParms (name) {
let url = window.location.href;//获取请求进来的完整url
let tstr = url.substring(url.indexOf(?) + 1).split(&);//先截取url的?后面的参数部分,在根据&分割成参数数组
let result = {};
tstr.forEach((item) => {
let res = item.split(=);//res为type,my-component1.vue。 res[0]为type,res[1为]my-component1.vue
result[res[0]] = res[1];//构造成键值对形式 res[0]为键,res[1]为值 例:type: "my-component1.vue"
})
return result[name];//通过键取值
}
var type = getUrlParms("type");//调用函数即可
