Vue动态查询key

Vue dynamic query key

我想使用所选设置url

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?
});

你应该用 [] 访问器包装密钥:

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

只是一个猜测:查询中的“key”:{ ...this.$route.query, key: value } 将被视为一个字符串,我想你希望“key”变量是改为使用。

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

但我也怀疑你想要 url 中的所有 key/values?所以你必须重组..

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