-3

How can I initiate this callback on Vue.js on page load?:

 lightGallery(document.getElementById('lightgallery')); 
Q-sup
  • 75
  • 1
  • 1
  • 7
  • Possible duplicate of [How to call a vue.js function on page load](https://stackoverflow.com/questions/40714319/how-to-call-a-vue-js-function-on-page-load) – str Oct 13 '17 at 07:54
  • I have tried the ready(), as well as just adding within script tags below. – Q-sup Oct 13 '17 at 07:57
  • 1
    I keep getting this error: lightGallery has not initiated properly – Q-sup Oct 13 '17 at 08:00

2 Answers2

1

One of Vue's life cycle hooks is beforeMount, Your code can be:

beforeMount(){
   lightGallery(document.getElementById('lightgallery')); 
},
haMzox
  • 2,073
  • 1
  • 12
  • 25
0

Use the initialization in your vue component with lifecycle hooks:

Vue.component('lightGallery', function() {
  template: '<div>Place template markup here</div>',
  mounted: function() {
    $(this.$el).lightGallery(); 
  }
});

Then you can just use the component:

<lightGalleryr></lightGallery>

There are different lifecycle hooks Vue provide:

I have listed few are :

  • beforeCreate: Called synchronously after the instance has just been initialized, before data observation and event/watcher setup.

  • created: Called synchronously after the instance is created. At this stage, the instance has finished processing the options which means the following have been set up: data observation, computed properties, methods, watch/event callbacks. However, the mounting phase has not been started, and the $el property will not be available yet.

  • beforeMount: Called right before the mounting begins: the render function is about to be called for the first time.

  • mounted: Called after the instance has just been mounted where el is replaced by the newly created vm.$el.

  • beforeUpdate: Called when the data changes, before the virtual DOM is re-rendered and patched.

  • updated: Called after a data change causes the virtual DOM to be re-rendered and patched.

You can have a look at complete list here.

You can choose which hook is most suitable to you and hook it to call you function like the sample code provided above.

tony19
  • 125,647
  • 18
  • 229
  • 307
Hasan Shahriar
  • 468
  • 3
  • 11