Vue Directives

linkaiyi
Follow

Define custom directives for binding, e.g. select2

option 1:

Vue.directive('selecttwo', {
  twoWay: true,
  bind: function () {
    $(this.el).select2()
    .on("select2:select", function(e) {
      this.set($(this.el).val());
    }.bind(this));
  },
  update: function(nv, ov) {
    $(this.el).trigger("change");
  }
});

use v-selecttwo for binding

option 2:

Vue.directive('select2', {
    inserted(el) {
        $(el).on('select2:select', () => {
            const event = new Event('change', { bubbles: true, cancelable: true });
            el.dispatchEvent(event);
        });

        $(el).on('select2:unselect', () => {
            const event = new Event('change', {bubbles: true, cancelable: true})
            el.dispatchEvent(event)
        })
    },
});

use v-select2 for binding

https://v2.vuejs.org/v2/guide/custom-directive.html

Object has 0 attachments

Was this article helpful?

This article is viewed 55 times!

Recent Viewed Articles

Related Articles

0 Comments

Leave a Comment

Support