Lifecycle methods are an important part of any Vue.js application. They give you the ability to perform certain actions at specific points in the lifecycle of your component. The Mounted method is one of the most commonly used ones, and allows you to run a function when your component is created.

In this article, we will be discussing the Mounted lifecycle hook in Vue.js. We will see what it is used for, and how we can leverage it in our applications in order to call a function when our app is first created.

By the end of this article, you should have a good understanding of the Mounted method and when and how to use it.

What are lifecycle methods in Vue?

Lifecycle methods are functions that you can choose to run at specific moments during the lifecycle of a component. They are very similar to React Hooks, so if you’re familiar with that library you probably won’t have any problem adjusting to them.

There are a number of different methods, and each one is designed to be run at a different point in the process.

They are built in Vue.js features, and give you a great deal of control over when your code will run a specific function. Here are the 8 lifecycle methods in order:

  1. Before create
  2. Created
  3. Before mount
  4. Mounted
  5. Before update
  6. Updated
  7. Before destroy
  8. Destroyed

Technically, we could use any of the first four in order to run one of our functions as soon as a component is created. However, in most cases we end up using the Mounted function due to it being a convention.

What is, then, the Mounted hook?

The Mounted hook is one of the lifecycle hooks that we can use in Vue.js. It is called after the instance has been mounted, which means that all child components have also been mounted. This is a good place to perform any operations that need to happen after the component has been rendered, such as fetching data or calling an API.

For that reason, it is often used to call a function as soon as a component is created.

How do we use the Mounted hook?

In order to use the Mounted hook, we simply need to define a function that we want to run, and then specify that this function should be called in the “mounted” lifecycle hook. Here is a very simple example that will log the message “Hello world!” as soon as we mount our component:

<script>
export default {
  mounted() {
    console.log("Hello World!");
  },
};
</script>

Once we have defined our function, we can then add the code that we want to run inside of it. For example, if we wanted to fetch some data from an API endpoint, we could do something like this:

<script>
export default {
  methods: {
    async getData() {
      const res = await fetch("https://myapi.com");
      const finalRes = await res.json();
    },
  },
  mounted() {
    this.getData();
  },
};
</script>

By using the code above, the tasks inside of our function will be executed after the component has been mounted. This means that all child components will also be mounted before our code runs.

This is important to keep in mind if you are trying to access a child component from inside of your function – you may need to make sure that the child component has finished mounting first, which is one of the reasons why we use the Mounted method instead of some of the earlier ones like Before Create.

Do we need the Mounted method with the Composition API?

If you’re using the new Composition API to run your app, you actually don’t need to do anything special to call a function when a component is first rendered. You could just write something like this:

<script setup>
function sayHi() {
  console.log("Hello World!");
}

sayHi();
</script>

However, if you want to have complete control over your code, you are able to call one of the lifecycle methods and add any code you want inside of it as a callback, like this:

<script setup>
import { onMounted } from "vue";
onMounted(() => {
  console.log("Hello World!");
});
</script>

Notice how the method has a slightly different name, but it still works exactly the same as it does with the Options API.

In any case, the Mounted hook in Vue.js allows us to run code after our component has been fully rendered and all child components have been mounted. This can be useful for data fetching or other operations that need to happen after the component has been rendered. So, if you’re trying to call a function on component creation, this is probably the tool you need!

👋 Hey, I'm Alejandro Rodríguez
Hey there! I'm a front end developer from Spain and a web dev teacher at Ironhack. I've been writing for more than 7 years as a side project, and I love to mix both disciplines whenever I can. Besides working, I'm also passionate about travelling (often with my laptop as a digital nomad) and water sports. What could be better than writing about code in a Caribbean beach, or from a coworking space in an european city?

💬 Leave a comment

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

We will never share your email with anyone else.