简体   繁体   English

如何将 v-modal 作为 Axios 参数的数组发送?

[英]How can I sent v-modal as array for Axios params?

I have a Vue Multiselect instance that's working properly, but my v-model is an array (since multiple options can be selected).我有一个正常工作的 Vue Multiselect 实例,但我的 v-model 是一个数组(因为可以选择多个选项)。 The issue is that I want to call a route within the loadExtraOptions method that will except the array of IDs as a param (The ids would be the catNum value for each index in the array).问题是我想在loadExtraOptions方法中调用一个路由,它将除了 ID 数组作为参数之外(ID 将是数组中每个索引的 catNum 值)。 I have params set in the axios call but it is currently just the model as a whole, how can I properly send the array of IDs in for that param?我在 axios 调用中设置了参数,但它目前只是整个模型,如何正确发送该参数的 ID 数组?

<multiselect
v-model="categoryModel"
:options="categoryOptions"
:multiple="true"
placeholder="Select Categories"
:close-on-select="true"
label="catNum"
track-by="catNum"
@select="loadExtraOptions"
></multiselect>

var vm = 
new Vue({
  el: "#app",
  props: { 

  },
  components: {Multiselect: window.VueMultiselect.default},
  data: {
        categoryModel: [],
        categoryOptions: [],
  },
  methods: {
    loadExtraOptions(){
      console.log(this.categoryModel);
      if(categoryModel.length > 0){
         axios.get('/test/route/autocomplete/category',{
            params: {
              categories:this.categoryModel
            }
         })
         .then((res) => {
              
            })
            .catch((error) => {
              
            });
      }
    },
 }
});

With .map you can only extract the id from the model.使用.map您只能从模型中提取 id。

this.categoryModel.map(category => category.id)

The GET request send parameters as a query string. GET请求将参数作为查询字符串发送。 So you need to send the ids as a string, not as array, as in the format below.因此,您需要将 id 作为字符串而不是数组发送,如下所示。

// For example,
// categoryModel: [{id: 1}, {id: 2}, {id: 3}]
// result: 1,2,3
this.categoryModel.map(category => category.id).join(',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM