raklos
raklos

Reputation: 28545

Dynamic query string name in vue js

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

Upvotes: 1

Views: 728

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

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

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

Upvotes: 2

Related Questions