Vue in Vue is best learned by connecting the rule to a small admin screen. Start with the smallest component or composable, observe the output, and then add one realistic constraint so the concept becomes practical.
The key habit for this lesson is to watch ref, prop, slot, or emitted event as it changes. That makes the topic easier to debug, easier to explain in interviews, and easier to use in real code without memorizing isolated syntax.
Vue's built-in <Transition> component applies enter/leave animations to a single element or component. It automatically adds CSS classes at the right moments, or you can use JavaScript hooks for full control.
| Class | When applied |
|---|---|
| v-enter-from | Start state of enter - added before element is inserted |
| v-enter-active | Active state of enter - applied during entire enter phase |
| v-enter-to | End state of enter - added after element is inserted |
| v-leave-from | Start state of leave |
| v-leave-active | Active state of leave |
| v-leave-to | End state of leave |
<template>
<div>
<button @click="show = !show">Toggle</button>
<!-- Basic fade transition -->
<Transition name="fade">
<p v-if="show">Hello, I fade in and out!</p>
</Transition>
<!-- Slide transition -->
<Transition name="slide">
<div v-if="show" class="panel">Sliding panel</div>
</Transition>
<!-- Mode: out-in (leave first, then enter) -->
<Transition name="fade" mode="out-in">
<component :is="currentView" :key="currentView" />
</Transition>
<!-- appear: animate on initial render -->
<Transition name="fade" appear>
<p>I animate when the page loads</p>
</Transition>
</div>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
const currentView = ref('HomeView')
</script>
<style>
/* Fade transition */
.fade-enter-active,
.fade-leave-active {
transition: opacity 0.3s ease;
}
.fade-enter-from,
.fade-leave-to {
opacity: 0;
}
/* Slide transition */
.slide-enter-active {
transition: all 0.3s ease-out;
}
.slide-leave-active {
transition: all 0.3s ease-in;
}
.slide-enter-from {
transform: translateX(-100%);
opacity: 0;
}
.slide-leave-to {
transform: translateX(100%);
opacity: 0;
}
/* Scale + fade */
.scale-enter-active,
.scale-leave-active {
transition: all 0.25s ease;
}
.scale-enter-from,
.scale-leave-to {
transform: scale(0.9);
opacity: 0;
}
</style>
<!-- TransitionGroup - animate lists -->
<template>
<div>
<input v-model="newItem" @keyup.enter="addItem" placeholder="Add item" />
<TransitionGroup name="list" tag="ul">
<li v-for="item in items" :key="item.id">
{{ item.text }}
<button @click="removeItem(item.id)">x</button>
</li>
</TransitionGroup>
</div>
</template>
<script setup>
import { ref } from 'vue'
const newItem = ref('')
const items = ref([
{ id: 1, text: 'Learn Vue' },
{ id: 2, text: 'Build something' },
])
function addItem() {
if (!newItem.value.trim()) return
items.value.push({ id: Date.now(), text: newItem.value })
newItem.value = ''
}
function removeItem(id) {
items.value = items.value.filter(i => i.id !== id)
}
</script>
<style>
/* List item enter/leave */
.list-enter-active,
.list-leave-active {
transition: all 0.4s ease;
}
.list-enter-from {
opacity: 0;
transform: translateX(-30px);
}
.list-leave-to {
opacity: 0;
transform: translateX(30px);
}
/* Smooth repositioning of remaining items */
.list-move {
transition: transform 0.4s ease;
}
/* Prevent layout shift during leave */
.list-leave-active {
position: absolute;
}
</style>
<!-- JavaScript hooks - full control with GSAP or Web Animations API -->
<template>
<Transition
@before-enter="onBeforeEnter"
@enter="onEnter"
@after-enter="onAfterEnter"
@enter-cancelled="onEnterCancelled"
@before-leave="onBeforeLeave"
@leave="onLeave"
@after-leave="onAfterLeave"
:css="false"
>
<div v-if="show" class="box">Animated box</div>
</Transition>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
// :css="false" - disable CSS transitions, use JS only
function onBeforeEnter(el) {
el.style.opacity = '0'
el.style.transform = 'scale(0.8)'
}
function onEnter(el, done) {
// Use Web Animations API
el.animate([
{ opacity: 0, transform: 'scale(0.8)' },
{ opacity: 1, transform: 'scale(1)' }
], {
duration: 400,
easing: 'ease-out'
}).onfinish = done // call done() when animation completes
}
function onLeave(el, done) {
el.animate([
{ opacity: 1, transform: 'scale(1)' },
{ opacity: 0, transform: 'scale(0.8)' }
], {
duration: 300,
easing: 'ease-in'
}).onfinish = done
}
function onAfterEnter(el) { console.log('Enter complete') }
function onAfterLeave(el) { console.log('Leave complete') }
function onBeforeLeave(el) { /* ... */ }
function onEnterCancelled(el) { /* ... */ }
</script>
Understanding Transitions is not just about syntax. In production applications, this topic directly affects maintainability, debugging speed, and team collaboration. Focus on readability, small reusable patterns, and predictable state flow when implementing Transitions.
A practical approach is to first implement the simplest working version, then refactor into reusable pieces (components/composables/stores) only when duplication appears. This helps keep your Vue codebase clean while avoiding over-engineering.
Use Vue when the program needs a clear answer to a specific problem, not because the keyword looks familiar. In a real Vue task, first name the input, then name the transformation, then name the output. This small discipline shows whether the topic is being used correctly or only copied from an example.
A reliable practice flow is: create the smallest working component or composable, add one normal case, add one edge case such as missing, repeated, empty, or boundary input, and then confirm the result with Vue Devtools and template warnings. If the result surprises you, reduce the code until the behavior is visible again.
The most common trap here is copying the syntax before understanding the behavior. Avoid it by writing one sentence before the code that explains why Vue is the right choice. After the code runs, verify the lesson by doing this: change one input and explain the changed output.
Copying the syntax before understanding the behavior.
Write the expected behavior first, then make the example prove it.
Practicing only the perfect input.
Also test missing, repeated, empty, or boundary input before considering the lesson complete.
Looking only at the final output.
Trace ref, prop, slot, or emitted event through each important step.
Use it when the problem matches the behavior shown in the example and when the result can be verified through Vue Devtools and template warnings.
Start with a tiny case, then test missing, repeated, empty, or boundary input. The main warning sign is copying the syntax before understanding the behavior.
Trace ref, prop, slot, or emitted event, predict the result, run the example, and compare your prediction with the actual output.
Explore 500+ free tutorials across 20+ languages and frameworks.