When doing SELECT2 plug-in encountered some pits, the final solution is as follows:
Vue.directive (' Select2 ', {inserted:function(EL, binding, Vnode) {varOptions = Binding.value | | {}; varDefaultopt ={placeholder:"--Please Choose--", Allowclear:true }; Options=_.assign (defaultopt, Options); $ (EL). Select2 (Options). On ("Select2:select", (e) = = { //V-model looks for //-an event named ' Change ' //-A value with the property path "$event. Target.value"El.dispatchevent (NewEvent (' Change ', {target:e.target});//Two-way binding does not take effect //events that bind the selected optionOptions && Options.onselect &&Options.onselect (e); }); //allowclear: Clear Selected$ (EL). Select2 (Options). On ("Select2:unselecting", (e) + = { //clear this value, which is the value of the VUEJS model bindingE.target.value = ""; El.dispatchevent (NewEvent (' Change ', {target:e.target})); //Two-way binding does not take effect }); //event triggered when binding Select2 dom rendering is completeOptions && Options.oninit &&Options.oninit (); }, Update:function(EL, binding, Vnode) {$ (EL). Trigger ("Change"); } });
Vuejs Custom Select2 directives