简体   繁体   English

JQuery 不适用于 Vuejs

[英]JQuery not working with Vuejs

I'm trying to add a JQuery plugin, owl carousel to a list that rendered using Vuejs.我正在尝试将 JQuery 插件 owl carousel 添加到使用 Vuejs 呈现的列表中。

HTML 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 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/您可以从以下位置找到完整代码: https : //jsfiddle.net/v18yjmuq/12/

I seem JQuery is complete it's execution before Vuejs rendered the list.我似乎 JQuery 在 Vuejs 呈现列表之前完成了它的执行。 How to avoid that issue?如何避免这个问题? Can I run JQuery after fully rendered the Vuejs for loop items?在完全呈现 Vuejs for 循环项后,我可以运行 JQuery 吗?

You should use Vue.nextTick when using a jQuery plugin that needs the DOM to be ready.在使用需要准备好 DOM 的 jQuery 插件时,您应该使用Vue.nextTick

From the vue.js documentation:来自 vue.js 文档:

Defer the callback to be executed after the next DOM update cycle.推迟在下一个 DOM 更新周期后执行的回调。 Use it immediately after you've changed some data to wait for the DOM update.在您更改一些数据以等待 DOM 更新后立即使用它。

In your case you should use the following implementation of the ready() method:在您的情况下,您应该使用 ready() 方法的以下实现:

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

EDIT: For Vue 2 use mounted() or created()编辑:对于 Vue 2 使用mounted()created()

Add ref prop to #user element like this像这样将 ref prop 添加到 #user 元素

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

Then in add mounted method to Vue component:然后在Vue组件中添加mounted方法:

...
mounted: function(){
  jQuery(this.$refs.carousel_or_anything).owlCarousel();
}
...

Vue.nextTick will work in most of the cases, alternatively, you can write your code in the built-in updated() method. Vue.nextTick 在大多数情况下都可以工作,或者,您可以在内置的 updated() 方法中编写代码。

updated(){
   // the method called when DOM gets updated
   // write jquery code here 
}

This is really interesting.这真的很有趣。 I think its taking some time to render the DOM and hence carousal is failing.我认为渲染 DOM 需要一些时间,因此 carousal 失败了。 Here I have added a setTimeout to add negligible delay and its working:在这里,我添加了一个setTimeout以添加可忽略的延迟及其工作:

https://jsfiddle.net/v18yjmuq/13/ https://jsfiddle.net/v18yjmuq/13/

ready: function() {
    this.listUsers();
    var self = this;
    setTimeout(function() {
      self.installOWLcarousel();
    }, 0);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM