MaBbKhawaja
MaBbKhawaja

Reputation: 902

Call 'created' function from 'methods' in VueJS after page is loaded

I AM NEW TO VUE JS My VueJS app structure goes like this

<template>
  <div>
    //HTML CODE
  </div>
</template>

<script>
export default {
  data() {
    return {

  },
  methods: {
   functionABC(){
   // CAN I CALL created() in this function ?
   }
  },
  created() {
    //SOME FUNCTIONS
  }
};
</script>

<style scoped>

</style>

I know that I can make a function of all the operations happening on first page load in created() and then call that function everytime i need and that will also serve the purpose but I actually want to know the proper way to call the created() function even after the page is loaded

Upvotes: 0

Views: 384

Answers (2)

Reinier68
Reinier68

Reputation: 3252

Well, if you use Vue-router, every time you switch to a different route, the old route (and its component) get's destroyed (unless you use the <keep-alive> tag). That means that if you have 2 routes and switch back and forth, the created() method gets called with every new route hit.

For demonstration I made a codesandbox. In the sandbox there are 2 components: Hello.vue and Goodbye.vue. In both of them there is a console.log. If you switch between the routes, you'll see that every time you switch, the message gets logged to the console. Meaning that the created method get's called.

Upvotes: 0

Alejandro
Alejandro

Reputation: 21

I think that you can't. If there is a way to do it I think that is not recommended. You should do a function and call it from created or from another function to not duplicate code

Upvotes: 2

Related Questions