Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery not working with Vuejs

I'm trying to add a JQuery plugin, owl carousel to a list that rendered using Vuejs.

HTML

<h4>1. Vuejs rendered items with OWL Carousel (not working)</h4>
<div id="user" class="owl-carousel">
    <div class="item" v-for="user in users">{{ user.name }}</div>
</div>

<h4>2. Pure HTML with OWL Carousel (working)</h4>
<div class="owl-carousel">
    <div class="item">Sunny</div>
    <div class="item">Michel</div>
    <div class="item">Daneil</div>
    <div class="item">Sony</div>
</div>

JS

var list = new Vue({
    el: '#user',
    data: {
        users: []
    },
    methods: {
        listUsers: function() {
            var users = [
            {
                id: 1,
                name: 'John'
            },
            {
                id: 2,
                name: 'Deo'
            },
            {
                id: 3,
                name: 'Anjela'
            },
            {
                id: 4,
                name: 'Samantha'
            }
            ];
            this.$set('users', users);
        },

        installOWLcarousel: function() {
            $('.owl-carousel').owlCarousel();
        }
    },
    ready: function() {
        this.listUsers();
        this.installOWLcarousel();
    }
});

You can find the entire code from: https://jsfiddle.net/v18yjmuq/12/

I seem JQuery is complete it's execution before Vuejs rendered the list. How to avoid that issue? Can I run JQuery after fully rendered the Vuejs for loop items?

like image 314
Renjith Avatar asked Aug 19 '16 11:08

Renjith


People also ask

Can jQuery work with Vue?

Finally, we can now install and use jQuery in Vue. I believe using jQuery in your application will probably save a lot of time for you. But, it might have performance issues. So, I recommend using vue packages that solve the same issue.

Can Vuejs replace jQuery?

But, what you might not know is that you can replace JQuery with Vue. In simple words, you can incorporate Vue just like you incorporate JQuery, without any build steps.

Is jQuery needed for Vue?

Vue -like frameworks - which make extensive use of data-binding - don't typically need frameworks like jQuery. The whole point of these kind of frameworks is that they build the HTML view for you based on the bound data.


2 Answers

You should use Vue.nextTick when using a jQuery plugin that needs the DOM to be ready.

From the vue.js documentation:

Defer the callback to be executed after the next DOM update cycle. Use it immediately after you’ve changed some data to wait for the DOM update.

In your case you should use the following implementation of the ready() method:

ready: function() {
    this.listUsers();
    Vue.nextTick(function () {
        this.installOWLcarousel();
    }.bind(this))
 }

EDIT: For Vue 2 use mounted() or created()

like image 95
demianh Avatar answered Oct 13 '22 07:10

demianh


Add ref prop to #user element like this

<div id="user" class="owl-carousel" ref="carousel_or_anything">

Then in add mounted method to Vue component:

...
mounted: function(){
  jQuery(this.$refs.carousel_or_anything).owlCarousel();
}
...
like image 43
Cong Nguyen Avatar answered Oct 13 '22 05:10

Cong Nguyen