Integrate with third-party libraries, manage animations, and handle resource cleanup
Use Case: Initialize a chart library when the element is mounted and clean it up when unmounted.
Hooks Used: @mounted, @updated, @unmounted
methods: { initChart() { // Initialize chart when element is mounted this.$nextTick(() => { console.log('Chart initialized'); }); }, updateChart() { // Update chart when data changes console.log('Chart updated with new data'); }, destroyChart() { // Clean up chart resources console.log('Chart destroyed'); } }
Use Case: Track when list items are added or removed, useful for animations and logging.
Hooks Used: @mounted, @unmounted
<!-- Each item tracks its lifecycle --> <div v-for="item in items" :key="item.id" @mounted="onItemMounted" @unmounted="onItemUnmounted"> {{ item.name }} </div>
Use Case: Apply enter/leave animations using CSS transitions with lifecycle hooks.
Hooks Used: @mounted, @unmount
This card fades and slides in when shown, and slides out when hidden.
Using CSS transitions with lifecycle hooks for smooth animations!
/* CSS Transitions */ .fade-slide-enter { opacity: 0; transform: translateY(-30px); } .fade-slide-enter-active { transition: all 0.5s ease-out; } // JavaScript - Two-phase animation methods: { onCardMount(el) { // Phase 1: Set initial state BEFORE DOM insertion el.classList.add('fade-slide-enter'); }, onCardEnter(el) { // Phase 2: Trigger transition AFTER DOM insertion el.offsetHeight; // Force reflow requestAnimationFrame(() => { el.classList.add('fade-slide-enter-active'); el.classList.remove('fade-slide-enter'); }); } }
Real-time log of all lifecycle events. Watch how hooks are called when elements are added, updated, or removed.