如何为url添加参数,要确保旧的参数中不含有新加的参数?

export function urlAddParams(params) {
  let str = '';
  let keys = Object.keys(params);
  let len = keys.length - 1;
  keys.forEach((key, index) => {
    str += key + '=' + params[key] + (index < len ? '&' : '');
  });
  let { origin, pathname, search, hash } = window.location;
  search = search === '' ? '?' : search + '&';
  return origin + pathname + search + str + hash;
}

你可能感兴趣的:(javascript前端)