Here is component that is working for me. Reference to their select2
https://v2.vuejs.org/v2/examples/select2.html
The gotcha for me was using change.select2 instead of triggering change event in the watch. change event causes infinite loop.
Your callback/ajax URL will need to return data with at least .id and .text properties. See this URL for proper format https://select2.org/data-sources/formats
<select2 v-model="member" name="member" callback="/res/member/select2.php" placeholder="Type a name"></select2>
Vue.component('select2', {
props: ['name', 'value', 'required', 'callback', 'placeholder'],
template: '<select :name="name" v-bind:class="{required: required}" class="vue-select2"></select>',
watch : {
value : function(value) {
$(this.$el).empty().append('<option value="' + value.id + '">' + value.text +'</option>').trigger('change.select2');
}
},
mounted: function() {
var that = this;
var options = {
width: '100%',
placeholder: this.placeholder,
allowClear: true,
ajax: {
url: this.callback,
dataType: 'json'
}
};
$(this.$el).select2(options);
$(this.$el).on('change', function() {
var item = $(this).select2('data')[0];
that.$emit('input', item);
});
}
});
mountedhook of the Vue instance and then pass the returned data to the component throughpropsas theoptionsprop in that fiddle.