I've looked around on here for answers and none of the already supplied ones work for me.
here is a working demo codepen.io/figaro/pen/mBvWJa
My project is simple, a one pager with Vue to handle the page routing. I have some jQuery based content on each different page. When I first load a page, the jQuery plugin displays fine. But when I navigate to other pages, the jQuery plugin does not get reloaded. I've tried every since hook I can think of...mounted, beforeRouteEnter, beforeRouteLeave, updated etc and none of them work. I've tried using this.$nextTick(function () {} and that does not work either.
The jQuery plugin is slick carousel. Yes I know there is an official vue slick wrapper for this, but I am not using the CLI....my project just has vue and vue router attached through a CDN and I don't understand how to use it with how my project is set up (their examples are for single file components). I don't care if the solution is hacky or not best practice, I just need to make Vue load the slick initializer script on each page. I saw examples to make a directive or component but they don't make sense to me since they are assuming you are using single file components which I am not. Is there any way to do this?
main.js
const NotFoundPage = {
name: "NotFoundPage",
template: "#404-template"
};
const routes = [
{ path: "/", component: Industry },
{ path: "/about", component: My Page },
{ path: "/contact", component: About},
{ path: "*", component: NotFoundPage }
];
const router = new VueRouter({
routes
});
new Vue({
router,
template: "#root-template",
mounted: function() {
this.$nextTick(function () {
})
},
beforeRouteUpdate ( to, from , next ) {
this.$nextTick(function () {
})
},
ready: function() {
this.$nextTick(function () {
})
},
updated () {
}
}).$mount("#app");
html
<div id="app"></div>
<template id="root-template">
...stuff
<view-router></view-router>
</template>
<template id="industry"></template> etc
jquery script
$('.gallery-responsive').slick({
infinite: true,
speed: 300,
slidesToShow: 3,
draggable: true,
edgeFriction: 0.30,
slidesToScroll: 1,
responsive: [{
breakpoint: 1100,
settings: {
slidesToShow: 2,
slidesToScroll: 1
}
}, {
breakpoint: 900,
settings: {
slidesToShow: 2,
slidesToScroll: 1
}
}, {
breakpoint: 800,
settings: {
slidesToShow: 1,
slidesToScroll: 1,
dots: false,
arrows: false,
}
}
]
});
here is a working demo codepen.io/figaro/pen/mBvWJa
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.
To navigate to a different URL, use router. push. This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.
Using Props To Share Data From Parent To Child # VueJS props are the simplest way to share data between components. Props are custom attributes that we can give to a component. Then, in our template, we can give those attributes values and — BAM — we're passing data from a parent to a child component!
A redirect means when the user visits /home , the URL will be replaced by / , and then matched as / . But what is an alias? An alias of / as /home means when the user visits /home , the URL remains /home , but it will be matched as if the user is visiting / .
I tried your demo and fixed it (check updated pen here) by using next
.
beforeRouteEnter(to, from, next) {
next(vm => {
$(".single-item").slick({
dots: true
});
})
}
The problem is that you tried to use slick before html elements in the component are completely established. In contrast, next
is invoked on the end of the navigation resolution flow.
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