Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/transition-router-view-appear.md
1---2title: RouterView Transitions Always Apply Despite Missing appear Prop3impact: LOW4impactDescription: Initial page load with RouterView triggers transition animation even without the appear prop due to async navigation5type: gotcha6tags: [vue3, transition, vue-router, appear, initial-load, navigation]7---89# RouterView Transitions Always Apply Despite Missing appear Prop1011**Impact: LOW** - When using `<Transition>` with Vue Router's `<RouterView>`, the enter transition animation runs on initial page load even if you haven't added the `appear` prop. This differs from normal Transition behavior where `appear` is required for initial render animations. This happens because Vue Router's navigations are asynchronous, causing the component to mount after the initial render.1213## Task Checklist1415- [ ] Be aware that RouterView transitions always animate on initial load16- [ ] If you want NO animation on initial load, you need to handle this explicitly17- [ ] Don't add `appear` prop expecting it to change behavior - it's already effectively enabled18- [ ] Consider whether initial animation is desired for your UX1920**Expected Behavior (Normal Transition):**21```vue22<template>23<!-- Without appear: No animation on initial render -->24<Transition name="fade">25<div v-if="show">Content</div>26</Transition>2728<!-- With appear: Animates on initial render -->29<Transition name="fade" appear>30<div v-if="show">Content</div>31</Transition>32</template>33```3435**RouterView Behavior (Different!):**36```vue37<template>38<!-- RouterView transitions ALWAYS animate on initial load -->39<!-- The appear prop has no effect here -->40<RouterView v-slot="{ Component }">41<Transition name="fade">42<component :is="Component" />43</Transition>44</RouterView>45</template>4647<!--48On initial page load:491. Vue renders the app502. Router resolves the route (async)513. Component mounts AFTER initial render524. Enter transition triggers (as if toggled from v-if="false" to v-if="true")53-->54```5556## Why This Happens5758Vue Router navigations are asynchronous. The sequence is:59601. Vue application mounts with empty RouterView612. Router resolves the initial route623. Route component is inserted into RouterView634. This insertion triggers the enter transition6465Since the component wasn't present in the initial render and is "inserted" afterward, Vue treats it as a normal enter transition, not an initial render.6667## If You Want to Disable Initial Animation6869```vue70<template>71<RouterView v-slot="{ Component }">72<Transition :name="isInitialLoad ? '' : 'fade'" mode="out-in">73<component :is="Component" />74</Transition>75</RouterView>76</template>7778<script setup>79import { ref, onMounted } from 'vue'80import { useRouter } from 'vue-router'8182const isInitialLoad = ref(true)83const router = useRouter()8485// After first navigation completes, enable transitions86router.isReady().then(() => {87// Small delay to ensure initial render is complete88setTimeout(() => {89isInitialLoad.value = false90}, 0)91})92</script>93```9495## Alternative: Use CSS to Skip First Animation9697```vue98<template>99<RouterView v-slot="{ Component }">100<Transition name="fade" mode="out-in">101<component :is="Component" :class="{ 'skip-initial': isInitialLoad }" />102</Transition>103</RouterView>104</template>105106<script setup>107import { ref } from 'vue'108import { useRouter } from 'vue-router'109110const isInitialLoad = ref(true)111const router = useRouter()112113router.isReady().then(() => {114isInitialLoad.value = false115})116</script>117118<style>119.fade-enter-active,120.fade-leave-active {121transition: opacity 0.3s ease;122}123124.fade-enter-from,125.fade-leave-to {126opacity: 0;127}128129/* Skip animation on initial load */130.skip-initial.fade-enter-active {131transition: none;132}133</style>134```135136## Standard RouterView Transition Pattern137138If you're fine with initial animation (often desired), use the standard pattern:139140```vue141<template>142<RouterView v-slot="{ Component, route }">143<Transition :name="route.meta.transition || 'fade'" mode="out-in">144<component :is="Component" :key="route.path" />145</Transition>146</RouterView>147</template>148149<style>150.fade-enter-active,151.fade-leave-active {152transition: opacity 0.3s ease;153}154155.fade-enter-from,156.fade-leave-to {157opacity: 0;158}159160/* Route-specific transitions via meta */161.slide-enter-active,162.slide-leave-active {163transition: transform 0.3s ease;164}165166.slide-enter-from {167transform: translateX(100%);168}169170.slide-leave-to {171transform: translateX(-100%);172}173</style>174```175176```javascript177// router.js178const routes = [179{180path: '/',181component: Home182},183{184path: '/about',185component: About,186meta: { transition: 'slide' } // Custom transition for this route187}188]189```190191## Reference192- [Vue Router Transitions](https://router.vuejs.org/guide/advanced/transitions.html)193- [Vue.js Transition appear](https://vuejs.org/guide/built-ins/transition.html#transition-on-appear)194