设置url的参数值

2022-01-26  本文已影响0人  抽疯的稻草绳
   updateUrl (key, value) {
      let newurl = this.updateQueryStringParameter(key, value)
      //向当前url添加参数,没有历史记录
      window.history.replaceState({
        path: newurl
      }, '', newurl);
    },



    updateQueryStringParameter (key, value) {
      let uri = window.location.href
      if (!value) {
        return uri;
      }
      let re = new RegExp("([?&])" + key + "=.*?(&|$)", "i");
      let separator = uri.indexOf('?') !== -1 ? "&" : "?";
      if (uri.match(re)) {
        return uri.replace(re, '$1' + key + "=" + value + '$2');
      }
      else {
        return uri + separator + key + "=" + value;
      }
    },

生命周期里 调用 设置参数为 inviteCode

   this.updateUrl("inviteCode", 28AT)
image.png

ps :

URLSearchParams

https://developer.mozilla.org/zh-CN/docs/Web/API/URLSearchParams

image.png
上一篇 下一篇

猜你喜欢

热点阅读