javascript - 使用 Vue Multiselect 在选择/输入时调用 Axios
问题描述
我有一个功能正常的 Vue Multiselect,我正在使用 axios 调用从我的数据库值中填充选项。这非常有效,允许我从现有选项中进行选择或输入新选项以创建标签。
事实上,这非常有效。但是如果可能的话,我需要一种方法来在每次用户选择和选项或按回车键保存标签选项时进行另一个 Axios 调用。有没有办法做到这一点?
这是我对 Vue 的第一次体验,我真的不确定这是否可行,但基本上我只是想知道每次选择或使用 enter 键输入标签时如何进行 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);
}
}
})
解决方案
监控@select 事件并触发您的 Axios 调用将发生的函数。
<div id="tagApp">
<multiselect
...
@select= "callAxios"
...
></multiselect>
</div>
...
methods: {
callAxios: function(val){
//your Axios call here
},
...
}
...
推荐阅读
- git - Github 将代码从旧仓库移动到新仓库,保留提交历史
- java - Cassandra 将 Token 转换为 long
- android - Libgdx 视频和音频记录器
- javascript - 如何在 QML 上使用 AnimeJS 库
- json - 如何使用 JQ 从 JSON 数组中提取 JSON 对象
- java - Spring Batch 3.0.10 - 使用 TaskExecutor 的并行步骤 - 不理解作业范围 bean
- r - 无法加载在 R4.0.2 包“XXXX”中安装包
- javascript - 解析方法并返回结果
- scala - 约束案例类和特征的类型参数
- javascript - 未捕获的 SyntaxError:await 仅在静态函数内的异步函数中有效