Home > Web Front-end > Vue.js > body text

The difference between created and mounted in vue

下次还敢
Release: 2024-05-02 21:03:15
Original
636 people have browsed it

The difference between created and mounted in Vue lies in execution time and data access capabilities: created: called when the component is initialized. It can only access Vue instance data and cannot interact with the DOM. mounted: Called after DOM is mounted. It can access Vue instance data and DOM elements and interact with DOM.

The difference between created and mounted in vue

The difference between created and mounted in Vue

In the life cycle of the Vue component, created and mounted are two crucial hook functions. They are used to perform specific tasks at different stages of a component, but they have some key differences in terms of execution time and availability.

Created

  • Instantiate or call this.$mount()# in new Vue() ## method is called immediately.
  • Can only access the data of the Vue instance (
  • this.data).
  • At this time, the DOM has not been rendered, so you cannot interact with DOM elements.

Mounted

    Called immediately after the DOM is mounted.
  • In addition to Vue instance data, you can also access DOM elements (
  • this.$el).
  • You can interact with DOM elements, such as manipulating styles, adding event listeners, or performing DOM queries.

Summary

FeaturescreatedmountedExecution timeDuring component initializationAfter DOM mountingData accessVue instance dataVue instance data and DOM elementsDOM interactionNoYes
In summary,

created is used to perform data-related tasks during the component initialization phase, while mounted is used during component loading Then perform DOM-related tasks.

The above is the detailed content of The difference between created and mounted in vue. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!