Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "mount" mean in Vue.js?

What does "mount" mean on using instance of vue.js to target a DOM element? (even in plain English?). For example in following:

This code creates a new instance of Vue and mounts it on the HTML element with the ID of app.

const app = new Vue().$mount('#app');

When the Vue instance has the el option, it automatically mounts to that element

like image 856
Mona Coder Avatar asked Mar 06 '18 18:03

Mona Coder


People also ask

What is Mount in VUE JS?

May 11, 2020. The mounted() hook is the most commonly used lifecycle hook in Vue. Vue calls the mounted() hook when your component is added to the DOM. It is most often used to send an HTTP request to fetch data that the component will then render.

What is the difference between mounted and created in Vue?

As such, any DOM manipulation, or even getting the DOM structure using methods like querySelector will not be available in created() . As mentioned in the article on lifecycle hooks, created() is great for calling APIs, while mounted() is great for doing anything after the DOM elements have completely loaded.

What mounted JS?

Mounting is the process of outputting the virtual representation of a component into the final UI representation (e.g. DOM or Native Components). In a browser that would mean outputting a React Element into an actual DOM element (e.g. an HTML div or li element) in the DOM tree.

What is after mounted Vue?

created() and mounted()in Vue.js The steps in Vue lifecycle are beforCreate, created, beforeMount, mounted, beforeUpdate, updated, beforeDestroy, destroyed. If the Vue instance is created created () hook allows you to add code to be run.


2 Answers

What is mounting in vue? In vue, every instance is first stored as Virtual DOM objects(virtual html elements) in memory.When Vue create those components(virtual DOM)visible to the real DOM(Actual html elements) , the moment in which it create virtual DOM into real DOM is call 'Mounting'. As the app state changes , vue detect changes user expect to see and put data changes to the real DOM from the memory.That is called an 'update'. The entire process is called Vue Lifescyclehooks which has four stages, namely create,mount,update and destroyed.

like image 129
Hung Om Avatar answered Sep 17 '22 23:09

Hung Om


Mounting takes place at the Virtual Dom Level, before the User sees anything.

When you $mount('#app'), there will be an 'el' parameter that gets set. This 'el' defines the ID of the element that this instance will be "mounted" to.

So, in your template, if you have an element that you want to be the mounted component, then in your declaration of the component, you would mount it with "el: #app".

VueJS Life-Cycle Diagram: https://vuejs.org/v2/guide/instance.html#Lifecycle-Diagram

Mounted Life-Cycle Hook: https://vuejs.org/v2/api/#mounted

like image 22
Marvin WordWeaver Parsons Avatar answered Sep 18 '22 23:09

Marvin WordWeaver Parsons