Complete the code to wrap the element with a Vue transition component.
<template> <[1] name="fade"> <p v-if="show">Hello Vue!</p> </[1]> </template>
The transition component in Vue is used to apply enter and leave transitions to elements or components.
Complete the code to bind the transition's CSS class name.
<template> <transition name="[1]"> <div v-if="visible">Content</div> </transition> </template>
The name attribute sets the base CSS class for the transition. Here, fade is commonly used for fade-in and fade-out effects.
Fix the error in the transition event listener syntax.
<template> <transition @[1]="onEnter"> <div v-if="show">Hello</div> </transition> </template>
The correct event name for the hook before the enter transition starts is before-enter.
Fill both blanks to create a fade transition with correct CSS classes.
<style> .[1]-enter-active, .[1]-leave-active { transition: opacity 0.5s; } .[2]-enter-from, .[2]-leave-to { opacity: 0; } </style>
The CSS classes must match the transition name attribute, here 'fade', to apply the fade effect correctly.
Fill all three blanks to create a transition with a custom JavaScript hook.
<template>
<transition
@[1]="beforeEnter"
@[2]="enter"
@[3]="leave"
>
<div v-if="show">Content</div>
</transition>
</template>Vue transition events include before-enter, enter, and leave for hooking into different phases of the transition.
Practice
<transition> component in Vue?Solution
Step 1: Understand the role of <transition>
The <transition> component is designed to wrap elements and apply animations when they appear or disappear.Step 2: Compare with other options
Fetching data, creating reactive properties, and handling events are unrelated to <transition> functionality.Final Answer:
To add animations when elements enter or leave the DOM -> Option BQuick Check:
Enter and leave animations [OK]
- Confusing transition with data fetching
- Thinking transition handles events
- Mixing transition with reactive data setup
Solution
Step 1: Check correct usage of <transition> with name
The <transition> component requires a name attribute to link CSS classes, and the element to animate must be inside it with v-if or v-show.Step 2: Validate each option
<transition name="fade"><div v-if="show">Content</div></transition> correctly wraps the element inside <transition name="fade"> with v-if. Using transition as an attribute on div is invalid, placing v-if on <transition> does not work properly, and using class on div without name on <transition> misses the name attribute.Final Answer:
<transition name="fade"><div v-if="show">Content</div></transition> -> Option AQuick Check:
Use <transition name="fade"> wrapper [OK]
- Putting v-if on <transition> instead of child
- Missing the name attribute on <transition>
- Using transition attribute on div instead of component
show changes from false to true?
<template>
<transition name="slide">
<div v-if="show">Hello</div>
</transition>
</template>
<style>
.slide-enter-active { transition: all 0.5s ease; }
.slide-enter-from { transform: translateX(-100%); opacity: 0; }
.slide-enter-to { transform: translateX(0); opacity: 1; }
</style>Solution
Step 1: Understand the transition classes
The classes slide-enter-from and slide-enter-to define the start and end states for entering. The div starts translated left and transparent, then moves to normal position and full opacity.Step 2: Analyze the effect when show changes true
When show becomes true, the div enters with the slide-enter transition, sliding in from left and fading in.Final Answer:
The div slides in from the left with fading effect -> Option DQuick Check:
slide-enter-from to slide-enter-to = slide in + fade [OK]
- Confusing enter and leave animations
- Ignoring transform and opacity in CSS
- Assuming no animation on v-if change
<transition name="fade">
<div v-show="visible">Content</div>
</transition>
<style>
.fade-leave-active { transition: opacity 0.3s ease; }
.fade-leave-from { opacity: 1; }
.fade-leave-to { opacity: 0; }
</style>Solution
Step 1: Examine the provided CSS classes
Only leave transition classes (.fade-leave-active, .fade-leave-from, .fade-leave-to) are defined. No enter classes (.fade-enter-active, .fade-enter-from, .fade-enter-to) are present.Step 2: Determine the impact
Missing enter classes (.fade-enter-*) means no animation occurs when the element enters (visible changes from false to true). The leave transition works fine with v-show.Final Answer:
Missing fade-enter classes causes no enter animation -> Option CQuick Check:
Missing enter classes = no enter animation [OK]
- Thinking v-show prevents leave animations (it doesn't)
- Proposing to change transition name
- Focusing on duration instead of missing classes
Solution
Step 1: Understand transition class roles
Enter classes control animation when element appears; leave classes control animation when element disappears.Step 2: To animate only removal, define leave classes only
By omitting enter classes, no animation runs on enter, but leave animations run on removal.Final Answer:
Use <transition> with only leave-active and leave-from/to CSS classes, no enter classes -> Option AQuick Check:
Only leave classes = animate removal only [OK]
- Using v-show prevents leave animations
- Defining enter classes triggers animation on add
- Confusing enter and leave class roles
Ready to prove it?
Pass the test and this pattern moves unknown
