简体   繁体   中英

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM