I am using vue.js and OwlCarousel and every time I use the v-for with the carousel it doesn't show me the style of the carousel it only show me the Images in column.
this the request to an API:
<script>
export default {
data: {
videos_p: []
},
mounted() {
axios.get("xxxxxxxxxxx")
.then(response => {
this.videos_p = response.data
console.log(this.videos_p)
})
}
}
$(document).ready(function() {
var owl = $('.owl-carousel');
owl.owlCarousel({
items: 4,
loop: true,
margin: 10,
autoplay: true,
autoplayTimeout: 900,
autoplayHoverPause: true,
responsiveClass: true,
responsive: {
0: {
items: 1,
nav: true
},
600: {
items: 3,
nav: false
},
1000: {
items: 5,
nav: true,
loop: false,
margin: 20
}
}
});
})
</script>
and this is the html:
<section id="demos">
<div class="row">
<div class="large-12 columns">
<div class="owl-carousel owl-theme">
<div class="item" v-for="video in videos_p">
<img v-bind:src="video.image_url">
</div>
</div>
</div>
</div>
</section>
but every time that I use vue it doesn't show me the carousel it only show me the images in column, but if I use the example like this it works fine:
<section id="demos">
<div class="row">
<div class="large-12 columns">
<div class="owl-carousel owl-theme">
<div class="item">
<img src="http://placehold.it/300x150&text=FooBar1">
</div>
<div class="item">
<img src="http://placehold.it/300x150&text=FooBar1">
</div>
<div class="item">
<img src="http://placehold.it/300x150&text=FooBar1">
</div>
<div class="item">
<img src="http://placehold.it/300x150&text=FooBar1">
</div>
<div class="item">
<img src="http://placehold.it/300x150&text=FooBar1">
</div> -->
</div>
</div>
</div>
</section>
any idea where I am making a mistake?????
If you're careful about how you do it, you can use jQuery and Vue together safely. In this article I'm going to demonstrate how to add the jQuery UI Datepicker plugin to a Vue project. And just to show off a bit, I'm even going to send data between this jQuery plugin and Vue! See it working in this JS Bin.
Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance's data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.
Installing and running Vue. js (“Vue”) can be done with a script tag that points to the Vue content delivery network (CDN). No build or development process is necessary.
What you are looking for is Vue-nextTick
What is happening is JS is executing your $().owlCarousel before axios is sending response, because you don't know when the response would be received you need to wait till the response from axios is received and then immediately execute the code in Vue.nextTick();
And the other problem is this
doesn't points to Vue instance in axios()
so you need to make a variable and store this
to it.
In your method, after you calling axios callback, add this:
var vm = this; //declare this just BEFORE calling axios();
// The below are inside axios.then()
vm.videos_p = response.data;
Vue.nextTick(function(){
$('.owl-carousel').owlCarousel({
items: 4,
loop: true,
margin: 10,
autoplay: true,
autoplayTimeout: 900,
autoplayHoverPause: true,
responsiveClass: true,
responsive: {
0: {
items: 1,
nav: true
},
600: {
items: 3,
nav: false
},
1000: {
items: 5,
nav: true,
loop: false,
margin: 20
}
}
});
}.bind(vm));
and then completely remove your $(document).ready();
I would had gone ahead and made a method: {}
in VueJS, and call it installOwlCarousel
and then call it in my process.nextTick()
.
Something like:
data: {
....
},
method: {
installOwlCarousel: function(){
$('.owl-carousel').owlCarousel({
//my options
});
},
mounted: function(){
var vm = this;
axios.get('...xx..')
.then((res)=>{
vm.videos_p = res.data;
Vue.nextTick(function(){
vm.installOwlCarousel();
}.bind(vm));
})
.catch((err)=>{
if(err) console.log(err);
});
}
}
Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With