I had the same problem when working on a large project, which had a bunch of existing jQuery code, where we wanted to switch over to Vue.JS, without having to refactor all of the code in one go.
If you are utilizing a module builder like webpack and you have no guarantee for the order of your imports you can do the following:
Once your root application has mounted, broadcast a custom event on the document:
const app = new Vue({
el: '#app',
mounted() {
$(document).trigger('vue-loaded');
}
});
And then wrap all of your jQuery code in an event listener for your custom event:
$(document).on('vue-loaded', function () {
const $toggle = $('.js-accordion-toggle');
$toggle.on('click', function () {
$(this).toggleClass('test');
});
});
This worked for me in a large application
Depending on your situation you might need to add $nextTick to ensure that everything has been rendered (https://v2.vuejs.org/v2/api/#mounted)
mounted () {
this.$nextTick(function () {
// Code that will run only after the
// entire view has been rendered
})
}