antchof
antchof

Reputation: 33

Vue dynamic query key

I wanna make url with choosen settings

const settings = this.getSettings();//returns object
Object.entries(settings).forEach(([key, value]) => {
       this.$router.push({ query: { ...this.$route.query, key: value } }); //why i can't access key here?
});

Upvotes: 2

Views: 694

Answers (2)

ZPiDER
ZPiDER

Reputation: 4412

just a guess: "key" in query: { ...this.$route.query, key: value } will be treated as a string, i guess you want the "key" variable to be used instead.

var query = { ...this.$route.query };
query[key] = value;
this.$router.push({ query: query });

but then i also suspect that you want all key/values in the url? so you'll have to restructure..

const settings = this.getSettings();
this.$router.push({ query: { ...this.$route.query, ...settings }});

Upvotes: 1

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You should wrap the key by [] accessor :

this.$router.push({ query: { ...this.$route.query, [key]: value } });

Upvotes: 2

Related Questions