Javascript 在vue multiselect中更改值时如何调用api
当vue multiselect中存在更改值事件时,如何调用api? 我希望在multiselect中更改值,调用api并在控制台中显示。下面是我的代码Javascript 在vue multiselect中更改值时如何调用api,javascript,vue.js,vuejs2,vue-component,Javascript,Vue.js,Vuejs2,Vue Component,当vue multiselect中存在更改值事件时,如何调用api? 我希望在multiselect中更改值,调用api并在控制台中显示。下面是我的代码 <template> <div> <multiselect v-model="value" placeholder="Select Your Currency" label="title" track-by="title" :options="options" :option-height
<template>
<div>
<multiselect v-model="value" placeholder="Select Your Currency" label="title" track-by="title" :options="options" :option-height="10" :show-labels="false">
<template slot="singleLabel" slot-scope="props"><img class="option__image" :src="props.option.img" alt="No Man’s Sky"><span class="option__desc"><span class="option__title">{{ props.option.title }}</span></span></template>
<template slot="option" slot-scope="props"><img class="option__image" :src="props.option.img" alt="No Man’s Sky">
<div class="option__desc"><span class="option__title" :id="props.option.id">{{ props.option.title }}</span><span class="option__small">{{ props.option.desc }}</span></div>
</template>
</multiselect>
</div>
</template>
<script>
import Vue from 'vue';
import Multiselect from 'vue-multiselect'
Vue.component('multiselect', Multiselect);
export default {
props: ['options'],
components: {
Multiselect
},
data () {
return {
value: { title: 'Bitcoin', img: 'https://coinnik.com/uploads/crypto-
logos/06fa2ab3d5a0f1d60e7d236aeccd6c6a.png' },
}
},
methods: {
}
}
</script>
html
如何定义我在更改项时使用的方法,然后调用api并在控制台内显示?您可以对值变量使用深度监视
watch: {
'value': {
handler: function (after, before) {
this.apiCalc();
},
deep: true
},
},
好的,首先,您可以使用
@input
事件来识别拾取的对象,如下所示:
然后在doApi中,可以检查当前值
doApi(){
console.log(this.value);
}
this.value的值将是您拾取的内容的数组。您可以将它们传递给API。完整代码打开此处:您是否尝试使用@select
事件?Vue Multiselect的文档包括几个您可以尝试的事件,请在此处查看:
所以试试看
<multiselect v-model="value" :options="options" @select="actionToExecute" multiple></multiselect>
然后在方法中定义它。我目前也在使用vue multiselect,请告诉我这是否适合您。我在使用multiselect,请帮助我解决此问题。我很抱歉,当我看到“multiselect”时,我假设是一个常规的HTML multiselect。现在添加新答案会很快。我最初也曾建议,但如果您这样做,删除项目时,您不会得到更新。我相信“输入”更好(正如我在回答中使用的那样)。我在另一个文件名multiselect.vue中定义了multiselect组件,我希望在app.js中使用该组件并调用api。然后更改控制台中app.js show中调用api的值
watch: {
'value': {
handler: function (after, before) {
this.apiCalc();
},
deep: true
},
},
<multiselect v-model="value" :options="options" @select="actionToExecute" multiple></multiselect>