具有标记和异步功能的vue-multiselect停止工作

时间:2018-09-29 15:47:47

标签: laravel vue.js vuejs2 axios vue-multiselect

我正在使用vue-multiselect,我希望用户能够使用async在数据库中搜索标签,如果他们找不到所需的标签,请输入自己的标签。这意味着我正在使用标记和异步。它可以按预期工作,直到我添加了在中找不到的标签,然后aysnc不再搜索。如果我删除了添加的标签,那么它将再次进行异步搜索。

false

我在另一个组件中两次使用了该组件:

<template>

    <div>
      <label class="typo__label" for="ajax">Async multiselect</label>
      <multiselect v-model="selectedTags" id="tags" label="name" track-by="code" placeholder="Type to search" open-direction="bottom" :options="tags" :taggable="true" @tag="addTag" :multiple="true" :searchable="true" :loading="isLoading" :internal-search="false" :clear-on-select="true" :close-on-select="false" :options-limit="300" :limit="3" :limit-text="limitText" :max-height="600" :show-no-results="false" :hide-selected="true" @search-change="asyncFind">
        <template slot="clear" slot-scope="props">
          <div class="multiselect__clear" v-if="selectedTags.length" @mousedown.prevent.stop="clearAll(props.search)"></div>
        </template><span slot="noResult">Oops! No elements found. Consider changing the search query.</span>
      </multiselect>
      <pre class="language-json"><code>{{ selectedTags  }}</code></pre>
    </div>

</template>


<script>
import axios from 'axios';
import Multiselect from 'vue-multiselect'

export default {
  components: {
    Multiselect
  },
  props: {
      userId: {
        type: Number,
        required: true
      },
    tagGroup: {
      type: String,
      required: true
    }
  },
  data () {
    return {
      selectedTags: [],
      tags: [],
      isLoading: false
    }
  },
  methods: {
    addTag (newTag) {
        const tag = {
          name: newTag
        }
        this.tags.push(tag)
        this.selectedTags.push(tag)
      },
      limitText (count) {
        return `and ${count} other tags`
      },
      asyncFind (query) {
        if( query.length > 3 ) {
          this.isLoading = true
         axios.get('/api/tags/'+this.tagGroup+'/'+query).then(response => {
            this.tags = response.data.results.map(a => {
               return { name: a.name.en };
            });
        })
       }
      },
      clearAll () {
        this.selectedTags = []
      }
  }
}

</script>

0 个答案:

没有答案