How to remove one query string from url?


#1

I am using the code below to append query strings as needed to the url

this.$router.push({ query: Object.assign({}, this.$route.query, { color }) });
this.$router.push({ query: Object.assign({}, this.$route.query, { size }) });

each query param correspond to a set of checkboxes and if none are checked then the respective query param should be removed from the url

I have looked into this.$router.replace() but I’m not sure if it is what I need.

Anyone have any suggestions?


#2

I just figured it out

let query = Object.assign({}, this.$route.query);
delete query.color;
this.$router.replace({ query });

this may not be the best way of doing it but it works for my case


#3

This one updates or deletes the search param. undefined is key here, null or other falsy value doesn’t behave the same!

router.replace({
  ...router.currentRoute,
  query: {
    search: state.search || undefined,
  }
})