首页 > 解决方案 > 使用动态名称作为参数 Vuejs

问题描述

我有多个参数,当我执行获取请求时,我只想拥有一个函数,该函数采用参数名称及其值。

问题是,参数是我定义的,而不是值。

this.debounce("search_fullname", 5);

debounce(searchField, value) {
  this.$router.push({
    query: {
     ...this.$route.query,
     searchField: value 
    }
  })
 }

在我得到的网址中,/?searchField=5而不是/?search_fullname=5

标签: vue.jsvuejs2

解决方案


this.debounce("search_fullname", 5);

debounce(searchField, value) {
  this.$router.push({
    query: {
      ...this.$route.query,
      [searchField]: value <--- Dynamic key in js object
    }
  })
}

你应该得到/?search_fullname=5而不是/?searchField=5在 url 中。


推荐阅读