Dynamic query string name in vue js

Viewed 383

I have a method like this which is called on click, 2 params are passed:

 selectOption(variantName, value) {
  this.$router.push({ query: { variantName: value } })
}

If I call selectOption with variantName = "size", and value ="small"

I want the url to be something like /foo?size=small

at the moment it is something like ?variantName=size

1 Answers

Since the key is dynamic you could wrap it by [] like :

 this.$router.push({ query: { [variantName]: value } })
Related