Difference between the created and mounted events in Vue.js

Javascriptvue.js

Javascript Problem Overview


Vue.js documentation describes the created and mounted events as follows:

created

> Called synchronously after the instance is created. At this > stage, the instance has finished processing the options which means > the following have been set up: data observation, computed properties, > methods, watch/event callbacks. However, the mounting phase has not > been started, and the $el property will not be available yet.

mounted

> Called after the instance has just been mounted where el is replaced > by the newly created vm.$el. If the root instance is mounted to an > in-document element, vm.$el will also be in-document when mounted is > called. > > This hook is not called during server-side rendering.

I understand the theory, but I have 2 questions regarding practice:

  1. Is there any case where created would be used over mounted?

  2. What can I use the created event for, in real-life (real-code) situation?

Javascript Solutions


Solution 1 - Javascript

created() : since the processing of the options is finished you have access to reactive data properties and change them if you want. At this stage DOM has not been mounted or added yet. So you cannot do any DOM manipulation here

mounted(): called after the DOM has been mounted or rendered. Here you have access to the DOM elements and DOM manipulation can be performed for example get the innerHTML:

console.log(element.innerHTML)

So your questions:

  1. Is there any case where created would be used over mounted?

Created is generally used for fetching data from backend API and setting it to data properties. But in SSR mounted() hook is not present you need to perform tasks like fetching data in created hook only

  1. What can I use the created event for, in real-life (real-code) situation?

For fetching any initial required data to be rendered(like JSON) from external API and assigning it to any reactive data properties

data:{
	myJson : null,
	errors: null
},
created(){
	//pseudo code
	database.get().then((res) => {
		this.myJson = res.data;
	}).catch((err) => {
		this.errors = err;
	});
}

Solution 2 - Javascript

For the created() hook, the data after manipulation in the browser it not shown in the DOM before mounted. In simple words the data takes time to manipulate the DOm seen the browser .

The mounted() hook is called after the DOM has been mounted or rendered which enables you to have access to the DOM elements and you can perform DOM manipulation.The best use for the mounted hook is if you need to access the DOM immediately before or after the initial render.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionlesssugarView Question on Stackoverflow
Solution 1 - JavascriptVamsi KrishnaView Answer on Stackoverflow
Solution 2 - JavascriptMuhammad NumanView Answer on Stackoverflow