Working with Vue can be awesome. But you might know that it can also give you headaches, when you try to combine it with jQuery plugins or other libraries.
The problem is that jQuery works fundamentally different than Vue. Vue renders everything based of the component’s core data. jQuery is mostly meant to do simple click handling and has powerful abilities manipulate the DOM.
When I looked for help myself I have found some unsatisfying solutions where components did do all the initialization work.
Let me show you how I would combine Vue.js and jQuery.
What’s the goal?
In most cases you can omit using jQuery and find a simple Vue based solution to your problem. Modals, sliders and so on are pretty simple with a Vue component and CSS.
So the goal is to use powerful jQuery plugins that cannot be written quickly in Vue.
We will …
- … use Vue directives to build a bridge to jQuery.
- … initialize the plugin when the element is attached.
- … destroy it when the element is detached.
- … send events to notify the component.
- … receive events from the component and pass them to the plugin.