Execute Function Upon Created In Vuejs Component

Nov 3, 2017

Refer to Vue.js lifecycle hooks

  • 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.
  • mounted: Called after the instance has been mounted, where el is replaced by the newly created vm.$el. If the root instance is mounted to an in-document element, vm.$el will also be in-document when mounted is called.

There is beforeCreate and beforeMount as well.

Sample code of listening to mounted in Vue.js Component.

<template>
  ...
</template>

<script>
export default {
  ...
  mounted: function () {
    // do something after element is mounted
  }
}
</script>

<style scoped>

</style>

You can listen to the events on Vue.js Instance as well.

new Vue({  data: {    ...  },  created: function () {  }})

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.