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

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.