Lifecycle Introduction
The lifecycle is the set of stages a component passes through. A component is created, drawn on the page, updated, and removed.
Mounting
mount starts the lifecycle for the whole app.
createApp(App).mount('#app')The first draw
The paragraph shows the starting message.
<p>{{ message }}</p>Example
<script setup>
import { ref } from 'vue'
const message = ref('The app is ready.')
</script>
<template>
<div>
<p>{{ message }}</p>
</div>
</template>
Key points
- Mounting means Vue draws the component on the page for the first time.
- Updating means Vue draws the component again after its data changes.
- Unmounting means Vue removes the component from the page.
- A lifecycle hook is a function that Vue calls at one stage.
- A hook is a function that runs at a set moment in code.
- Each hook runs at its own stage, at the right moment.
In this example, message is a ref with the text The app is ready. The template shows the message in a paragraph. The first render shows the message. This lesson uses no hook. The stages give the map, and the next three lessons add one hook at each stage.
Note
The next three lessons cover one hook each, in stage order.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What is mounting?