This article aims to explore the distinction between the ‘created’ and ‘mounted’ events in the Vue.js framework.
The ‘created’ event is invoked immediately after the instance is formed, serving to establish data observation, computed properties, methods, and watch/event callbacks. Primarily, it is utilized for retrieving initial data from an external API and assigning it to reactive data properties. This event is advised when there is no requirement for interaction with the Document Object Model (DOM) and occurs earlier than the ‘mounted’ event.
Conversely, the ‘mounted’ event is triggered after the instance has been mounted, granting access to DOM elements and enabling DOM manipulation. It is employed for executing actions subsequent to the initial rendering and frequently for accessing and modifying the DOM. Unlike the ‘created’ event, the ‘mounted’ event is not invoked during server-side rendering.
In summary, the ‘created’ event focuses on data setup and data retrieval, while the ‘mounted’ event concentrates on DOM manipulation and post-render actions.
Created Event
The ‘created’ event in Vue.js is called synchronously after the instance is created and is used for setting up data observation, computed properties, methods, and watch/event callbacks, making it suitable for tasks such as fetching data from an API backend.
This event is commonly used to trigger actions for API calls and fetch initial data from an external API. It is particularly useful when server-side rendering is not present and tasks like data fetching need to be performed. However, the ‘created’ event does not wait for asynchronous operations to complete before moving on to the next lifecycle stage.
It is recommended when there is no need to interact with the DOM and is called for every Vue instance. The ‘created’ event manipulates data in the browser but does not show it in the DOM before the ‘mounted’ event.
Mounted Event
Mounted event is triggered after the instance has been rendered and provides access to the DOM elements, allowing for immediate DOM manipulation.
Unlike the created event, the mounted event is not called during server-side rendering. It is useful for performing actions that require interaction with the DOM, such as accessing and manipulating DOM elements.
This event is commonly used for tasks like setting up third-party libraries, initializing plugins, or performing animations.
The mounted event is called after the data has been manipulated and is visible in the DOM. It is suitable for accessing and manipulating the DOM immediately before or after the initial render.
Overall, the mounted event plays a crucial role in the Vue.js lifecycle, enabling developers to interact with the DOM and perform actions after the initial render.
Comparison
In Vue.js, the ‘created’ and ‘mounted’ events serve distinct purposes in the application lifecycle.
The ‘created’ event is called synchronously after the instance is created and is commonly used for:
- Setting up data observation
- Computed properties
- Methods
- Watch/event callbacks
It is also useful for:
- Fetching initial data from an external API
- Assigning it to reactive data properties
However, the ‘created’ event does not wait for async operations to complete before moving on to the next lifecycle stage.
On the other hand, the ‘mounted’ event is called after the instance has been mounted or rendered, allowing access to DOM elements and enabling DOM manipulation. It is commonly used for:
- Performing actions after the initial render
- Accessing and manipulating the DOM.