Skip to main content
  1. Posts/

Should I use created() or mounted() in Vue?

·374 words·2 mins
Joe Erickson
Author
Joe Erickson
Senior software developer specializing in web development, AI, and helping others learn to code.
Table of Contents

All in all, the Vue lifecycle methods are fairly straight forward. There’s even a helpful Vue lifecycle chart that describes when the various methods are called. All of that is super helpful.

Until you want to use them. If I want to load in data from an API, which method should I use? Why does my component crash sometimes when I try to update DOM elements in created()? Which method should I use for what?

First of all, let’s remember when the methods are called as the page is loaded and our components are added.

The created() method will be called on your component after the component object is created but before it is put on the page. All of the component’s data, props, computed and methods will be available. The only thing that won’t be available is the template or any of the component’s DOM. There really is no view yet to speak of.

mounted() is called after the component’s DOM created in memory and is added to the page. mounted() is basically Vue saying, “I’m finished with this one.”

So, which one do I use?
#

So, with created() there is no view yet. And because there is no view, this is the perfect time to fetch data from an API or manipulate data passed in via props. Fetching data now means that there will be less of a delay from when the component is show to when the data shows up on the screen because the call will start before the component is rendered.1 When the view is shown, the data you’ve loaded will be shown as well.

So what is mounted() good for? It’s good for loading anything that manipulates the component’s DOM. Maybe it’s working with a plugin like Google Maps or a slide show library, mounted() is where you will have access to the this.$el variable–representing the component’s root element–and can load in those other libraries.

So most of the time, expect to use created() unless you run into a scenario where you need to have access to the DOM first.


  1. This cuts down on user perceived speed, but doesn’t actually speed up the API call. Thanks to @papa_john on dev.to for pointing out that this wasn’t clear. ↩︎

Related

How to show a loading icon before data is loaded in Vue and Vuex

·665 words·4 mins
UPDATE: I have a better way of handling this here. I would recommend that post if you’re looking to handle the status of your data loads from now on. I hate ambiguous lag. Is this site still loading or did it freeze? Am I waiting for something or is this it? Whenever a website has to load in information from an API, there is a chance that loading will appear painfully slow to the user. Human beings perceive an empty screen much differently than they perceive an animation on the screen.1 In order to make our website seem faster than it is, it’s always good to throw in a loading animation every time that we have to access a potentially slow resource like an API call.

How to save data to your API using Vuex

·603 words·3 mins
I talked before about how to query your back-end API from Vuex, but that was mainly centered on getting data. What happens when you want to save data to a back-end API after it is updated or added in the Vuex store? What’s the best, most efficient way to accomplish this?

What's the purpose of Vuex?

·679 words·4 mins
There are a lot of things to worry about when it comes to Vue applications. You’ve got SPAs and routing and web workers and Jest and Cypress and everything else. It gets overwhelming pretty quickly. The hard part is not learning these technologies, it’s knowing what their place is in the application.

How to query your API using Vuex in your Vue application

·866 words·5 mins
Once you start using Vuex to manage the shared data of your Vue application, it becomes less clear on where or how to call your back-end API. I think everyone starts out making API calls in the created function of your component. But that doesn’t scale past a handful of components. Then, they end up loading in the same data over and over again and each component has it’s own copy, which is impossible to keep in sync. You lose the efficiency of having one part of the system in control of the data when you do that and on a larger application, this very quickly falls apart.