【问题标题】:Axios call on select/enter with Vue Multiselect使用 Vue Multiselect 在选择/输入时调用 Axios
【发布时间】:2019-04-18 14:57:18
【问题描述】:

我有一个正常运行的 Vue Multiselect,我正在使用 axios 调用来填充我的数据库值中的选项。这非常有效,允许我从现有选项中进行选择或输入新选项以创建标签。

事实上,这非常有效。但是如果可能的话,我需要一种方法来在每次用户选择和选项或按回车键保存标签选项时进行另一个 Axios 调用。有没有办法做到这一点?

这是我第一次使用 Vue,我真的不确定这是否可行,但基本上我只是想知道每次选择或使用回车键输入标签时如何进行 axios 调用

<div id="tagApp">
  <multiselect
  label="tag_data"
  track-by="campaign_tag_id"
  v-model="value"
  :options="options"
  :multiple="true"
  :taggable="true"
  @tag="addTag"
  @search-change="val => read(val)"
  :preselect-first="false"
  :close-on-select="false" 
  :clear-on-select="true" 
  :preserve-search="true" 
  tag-placeholder="Add this as new tag" 
  placeholder="Search or add a tag"
  ></multiselect>
</div>

new Vue({
      components: {
        Multiselect: window.VueMultiselect.default
      },
      el: "#tagApp",
      data() {
        return{
            value: [],
            loading: false,
            options: []
        }

      },
      methods: {
        read: function(val){
            //console.log('searched for', val);
          if (val) {
            this.loading = true;
            this.options = [];

            const self = this;
            console.log(val);

            axios.get('campaigns/search',{params: {query: val}})
                .then(function (response) {
                    self.options = response.data;
                    console.log(response.data);
            });

          } else {
            this.options = [];
          }
        },
        addTag(newTag) {
          const tag = {
            tag_data: newTag,
          };
          this.options.push(tag);
          this.value.push(tag);
        }
      }
    })

【问题讨论】:

  • 你有什么问题?应该很简单..在选择拨打您的电话,或在标签添加拨打您的电话。自动完成的工作原理相同。
  • 我想我知道我可以在 this.value.push 行之后在当前的 addTag 函数中进行 axios 调用,但我想知道如何在 select 上执行此操作?
  • v-on:change 属性添加到您的多选并在那里拨打电话
  • 好的,所以如果我添加 v-on:change 并调用 axios,然后在 addTag 函数中也调用 axios,那么无论他们键入并输入新的还是从自动完成列表它会进行新的 axios 调用吗?

标签: javascript vue.js axios multi-select


【解决方案1】:

监控@select 事件并触发你的 Axios 调用将发生的函数。

<div id="tagApp">
  <multiselect
  ...
  @select= "callAxios"
  ...
  ></multiselect>
</div>
...

   methods: {
        callAxios: function(val){
            //your Axios call here
        },
        ...
   }

...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-18
    • 1970-01-01
    • 1970-01-01
    • 2020-12-13
    • 2018-02-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多