Categories
Vue Answers

How to call a Vue.js function on page load?

Spread the love

Sometimes, we want to call a Vue.js function on page load

In this article, we’ll look at how to call a Vue.js function on page load

How to call a Vue.js function on page load?

To call a Vue.js function on page load, we can run our code in the beforeMount hook.

For instance, we write

<script>
export default {
  methods: {
    getUnits() {
      //...
    },
  },
  beforeMount() {
    this.getUnits();
  },
};
</script>

to call this.getUnits in the beforeMount hook so getUnits runs when the page loads.

Conclusion

To call a Vue.js function on page load, we can run our code in the beforeMount hook.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *