Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to load Jquery plugin/script when Vue router changes page?

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

like image 250
Charlie Avatar asked Oct 26 '17 00:10

Charlie


People also ask

Can I use Vue and jQuery together?

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.

How do I navigate to another page in Vue?

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.

How do I move data from one page to another in Vue?

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!

What is redirect in Vue router?

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 / .


1 Answers

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.

like image 178
choasia Avatar answered Sep 30 '22 06:09

choasia