1.第二个页面没有这个参数的话,从第一个页面跳转到第二个页面,把参数传url上带过来
在main.js里面设置头部
//vue动态获取地址栏参数
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('=');
// decodeURIComponent()可对encodeURIComponent()函数编码的URI进行解码。
let key= decodeURIComponent(tempArr[0]);
let val= decodeURIComponent(tempArr[1]);
return obj[key] = val;
})
}
/**
* 初始化配置
*/
http.initHttp({
timeout: 1000 * 30,
baseURL: '/announce', // 后端的上下文配置:server.servlet.context-path, 若有值则必须填写其值
headers: {
'Content-Type': 'application/json',
'SOURCE':obj.source //这个参数
}
})
2.在第二个页面把这个参数传到url上就可以了,在第一个页面跳转页面传参数
goSearchList () {
//vue动态获取地址栏参数
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('=');
// decodeURIComponent()可对encodeURIComponent()函数编码的URI进行解码。
let key= decodeURIComponent(tempArr[0]);
let val= decodeURIComponent(tempArr[1]);
return obj[key] = val;
})
}
if (this.ytoSearchKey && this.ytoSearchKey.length > 0) {
this.$router.push({
path: 'searchList',
query: {
source:obj.source,//就是这个参数
ytoSearchKey: this.ytoSearchKey
}
})
}
}
网友评论