The preferred method of integrating jQuery plugins into Vue 2 is to wrap them in a component. Here is an example of your Chosen plugin wrapped in a component that handles both single and multiple selects.
Vue.component("chosen-select",{
props:{
value: [String, Array],
multiple: Boolean
},
template:`<select :multiple="multiple"><slot></slot></select>`,
mounted(){
$(this.$el)
.val(this.value)
.chosen()
.on("change", e => this.$emit('input', $(this.$el).val()))
},
watch:{
value(val){
$(this.$el).val(val).trigger('chosen:updated');
}
},
destroyed() {
$(this.$el).chosen('destroy');
}
})
And this is an example of usage in a template:
<chosen-select v-model='cities' multiple>
<option value="Toronto">Toronto</option>
<option value="Orleans">Orleans</option>
<option value="Denver">Denver</option>
</chosen-select>
<chosen-select v-model='cities2'>
<option value="Toronto">Toronto</option>
<option value="Orleans">Orleans</option>
<option value="Denver">Denver</option>
</chosen-select>
Fiddle for multiple select.
Original Answer
This component doesn't handle multiple selects correctly, but leaving it here because it was the original answer that was accepted.
Vue.component("chosen-select",{
props:["value"],
template:`<select class="cs-select" :value="value"><slot></slot></select>`,
mounted(){
$(this.$el)
.chosen()
.on("change", () => this.$emit('input', $(this.$el).val()))
}
})
This component supports v-model. So that you can use it in your template like so:
<chosen-select v-model='cities'>
<option value="Toronto">Toronto</option>
<option value="Orleans">Orleans</option>
</chosen-select>
Here is your fiddle updated.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…