简体   繁体   English

在 vue-multiselect 中正确跟踪选项

[英]Properly tracking options in vue-multiselect

I'm currently using an axios call to preform an auto-complete with Vue-multiselect.我目前正在使用 axios 调用通过 Vue-multiselect 执行自动完成。 I'm now getting my data back from the query and can see it when dumping it in the console, but I'm still not connecting somewhere as far as loading my array with options and tracking them in the multiselect component.我现在正在从查询中取回我的数据,并且可以在将其转储到控制台中时看到它,但是我仍然没有连接到某个地方,因为我无法使用选项加载我的数组并在多选组件中跟踪它们。

Right now, my response.data looks like this:现在,我的response.data如下所示:

0:
  contact: test@mail.com
1:
  contact: newmail@mail.com

I simply want the contact field to be what shows up as their select options and I also want that to be what I send in a later submission so I want to label AND track by that one field.我只是希望contact字段显示为他们的选择选项,我也希望它是我在以后提交中发送的内容,因此我想通过该字段标记和跟踪。

What am I doing wrong here?我在这里做错了什么?

<multiselect 
   v-model="copyUsers" 
   :options="copyUsersOptions" 
   @search-change="val => searchCopiedUsers(val)"
   :multiple="true" 
   :loading="loading"
   placeholder="Enter user(s) to be copied" 
   label="name"
   track-by="value">
 </multiselect>

 data() {
    return {
        loading: false,
         copyUsers: [],
         copyUsersOptions: [],
    }
 },
 methods: {
    searchCopiedUsers: function(val) {
          console.log('searched for', val);
          if (val) {
            this.loading = true;

            axios.get('/users/search',{params: {query: val}})
            .then(response => {
                
                this.copyUsersOptions = response.data;

                console.info(this.copyUsersOptions);

                
              })
              .catch(function(error) {
                this.loading = false;
                console.log(error)
              })
              .finally(function() {
                this.loading = false;
              })

          } else {
            this.copyUsersOptions = [];
          }
    },
}

Try casting your object to an array尝试将您的对象转换为数组

axios.get('/users/search',{params: {query: val}})
    .then(response => {
        this.copyUsersOptions = [...response.data];
    })

Currently, your result from your server is an object.目前,您的服务器的结果是一个对象。 Which from your initialization of vue-multiselect will not work.您对 vue-multiselect 的初始化将不起作用。 By converting it to an array using the spread operator , the array will be formatted correctly.通过使用扩展运算符将其转换为数组,数组将被正确格式化。

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

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