Skip to content

Commit a7b5c9c

Browse files
committed
lifecycle
1 parent 7cac95a commit a7b5c9c

File tree

4 files changed

+138
-139
lines changed

4 files changed

+138
-139
lines changed

src/.vitepress/config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,10 @@ const sidebar = {
131131
{ text: 'List Rendering', link: '/guide/essentials/list' },
132132
{ text: 'Event Handling', link: '/guide/essentials/event-handling' },
133133
{ text: 'Form Input Bindings', link: '/guide/essentials/forms' },
134+
{
135+
text: 'Lifecycle Hooks',
136+
link: '/guide/essentials/lifecycle'
137+
},
134138
{ text: 'Watchers', link: '/guide/essentials/watchers' },
135139
{ text: 'Template Refs', link: '/guide/essentials/template-refs' },
136140
{
@@ -146,10 +150,6 @@ const sidebar = {
146150
text: 'Registration',
147151
link: '/guide/components/registration'
148152
},
149-
{
150-
text: 'Lifecycle',
151-
link: '/guide/components/lifecycle'
152-
},
153153
{ text: 'Props', link: '/guide/components/props' },
154154
{ text: 'Non-Prop Attributes', link: '/guide/components/attrs' },
155155
{ text: 'Events', link: '/guide/components/events' },

src/guide/components/lifecycle.md

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/guide/essentials/lifecycle.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Lifecycle Hooks
2+
3+
Each Vue component instance goes through a series of initialization steps when it's created - for example, it needs to set up data observation, compile the template, mount the instance to the DOM, and update the DOM when data changes. Along the way, it also runs functions called lifecycle hooks, giving users the opportunity to add their own code at specific stages.
4+
5+
## Registering Lifecycle Hooks
6+
7+
For example, the <span class="composition-api">`onMounted`</span><span class="options-api">`mounted`</span> hook can be used to run code after the component has finished the initial rendering and created the DOM nodes:
8+
9+
<div class="composition-api">
10+
11+
```vue
12+
<script setup>
13+
import { onMounted } from 'vue'
14+
15+
onMounted(() => {
16+
console.log(`the component is now mounted.`)
17+
})
18+
</script>
19+
```
20+
21+
</div>
22+
<div class="options-api">
23+
24+
```js
25+
export default {
26+
mounted() {
27+
console.log(`the component is now mounted.`)
28+
}
29+
}
30+
```
31+
32+
</div>
33+
34+
There are also other hooks which will be called at different stages of the instance's lifecycle, with the most commonly used being <span class="composition-api">[`onMounted`](/api/composition-api-lifecycle.html#onmounted), [`onUpdated`](/api/composition-api-lifecycle.html#onupdated), and [`onUnmounted`](/api/composition-api-lifecycle.html#onunmounted). Some hooks The full reference for all lifecycle hooks and their respective use cases can be found [here](/api/composition-api-lifecycle.html).</span><span class="options-api">[`mounted`](/api/options-lifecycle.html#mounted), [`updated`](/api/options-lifecycle.html#updated), and [`unmounted`](/api/options-lifecycle.html#unmounted). All lifecycle hooks are called with their `this` context pointing to the current active instance invoking it. The full reference for all lifecycle hooks and their respective use cases can be found [here](/api/options-lifecycle.html).</span>
35+
36+
<div class="composition-api">
37+
38+
When calling `onMounted`, Vue automatically associates the registered callback function to the current active component instance. This requires these hooks to be registered **synchronously** during component setup. For example, do not do this:
39+
40+
```js
41+
setTimeout(() => {
42+
onMounted(() => {
43+
// this won't work.
44+
})
45+
}, 100)
46+
```
47+
48+
</div>
49+
50+
## Lifecycle Diagram
51+
52+
Below is a diagram for the instance lifecycle. You don't need to fully understand everything going on right now, but as you learn and build more, it will be a useful reference.
53+
54+
![Component lifecycle diagram](/images/lifecycle.svg)

0 commit comments

Comments
 (0)