美文网首页
第一个页面获取地址栏参数,传到header里面面,然后在第二个页

第一个页面获取地址栏参数,传到header里面面,然后在第二个页

作者: 流泪手心_521 | 来源:发表于2021-02-03 18:38 被阅读0次

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
          }
        })
      }
    }

相关文章

网友评论

      本文标题:第一个页面获取地址栏参数,传到header里面面,然后在第二个页

      本文链接:https://www.haomeiwen.com/subject/pjemtltx.html