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/v-else-must-follow-v-if.md
1---2title: v-else Must Immediately Follow v-if or v-else-if3impact: MEDIUM4impactDescription: Misplaced v-else causes a compile-time error in SFCs, or renders unconditionally with runtime template compilation5type: capability6tags: [vue3, conditional-rendering, v-if, v-else, v-else-if]7---89# v-else Must Immediately Follow v-if or v-else-if1011**Impact: MEDIUM** - A `v-else` or `v-else-if` element must immediately follow a `v-if` or `v-else-if` element in the DOM. If there's any element in between:1213- **SFC compilation (Vite/vue-loader):** Vue throws a **compile-time SyntaxError** - the code won't build at all14- **Runtime template compilation:** The element renders unconditionally (always visible), losing its conditional behavior1516In most modern Vue projects using Single File Components, this is caught at build time.1718## Task Checklist1920- [ ] Place v-else immediately after the v-if element (no elements in between)21- [ ] Place v-else-if immediately after v-if or another v-else-if22- [ ] Use `<template>` wrapper if you need to group multiple elements within a branch23- [ ] If you need content between conditions, restructure using nested conditionals or computed2425**Incorrect:**26```html27<!-- WRONG: v-else not immediately after v-if -->28<template>29<div v-if="isLoggedIn">30Welcome back!31</div>3233<p>Some other content in between</p>3435<div v-else>36<!-- This v-else is NOT recognized! It will ALWAYS render (unconditionally) -->37Please log in38</div>39</template>40```4142```html43<!-- WRONG: Comment or whitespace element between -->44<template>45<span v-if="status === 'loading'">Loading...</span>46<!-- This comment breaks the chain! -->47<span v-else-if="status === 'error'">Error occurred</span>48<span v-else>Done</span>49</template>50```5152```html53<!-- WRONG: Text node between conditions -->54<template>55<div v-if="showA">A</div>56Just some text here57<div v-else>B</div> <!-- Not recognized, always renders -->58</template>59```6061**Correct:**62```html63<!-- CORRECT: v-else immediately follows v-if -->64<template>65<div v-if="isLoggedIn">66Welcome back!67</div>68<div v-else>69Please log in70</div>7172<p>Some other content (placed after the conditional block)</p>73</template>74```7576```html77<!-- CORRECT: Full v-if/v-else-if/v-else chain -->78<template>79<span v-if="status === 'loading'">Loading...</span>80<span v-else-if="status === 'error'">Error: {{ errorMessage }}</span>81<span v-else-if="status === 'empty'">No data found</span>82<span v-else>{{ data }}</span>83</template>84```8586```html87<!-- CORRECT: Using <template> for multiple elements per branch -->88<template>89<template v-if="isLoggedIn">90<h1>Welcome back!</h1>91<p>Your dashboard</p>92<UserStats />93</template>94<template v-else>95<h1>Please log in</h1>96<LoginForm />97</template>98</template>99```100101```html102<!-- CORRECT: Restructure if you need content between conditions -->103<template>104<div class="conditional-section">105<div v-if="isLoggedIn">Welcome back!</div>106<div v-else>Please log in</div>107</div>108109<div class="always-shown">110Some other content111</div>112113<div class="another-conditional">114<div v-if="showMore">More info</div>115<div v-else>Click to expand</div>116</div>117</template>118```119120## Debugging Tips121122```html123<!-- If v-else is always visible (rendering unconditionally), check for: -->124125<!-- 1. Elements between v-if and v-else -->126<!-- 2. HTML comments that break the chain -->127<!-- 3. Text nodes (including whitespace in some cases) -->128<!-- 4. Component tags that render content between -->129130<!-- SFC compilation throws: "v-else/v-else-if has no adjacent v-if or v-else-if" (build fails) -->131<!-- Runtime template compilation shows a warning but renders the element unconditionally -->132<!-- Vue DevTools will show the v-else element as not part of the condition chain -->133```134135## Reference136- [Vue.js Conditional Rendering - v-else](https://vuejs.org/guide/essentials/conditional.html#v-else)137