Given the following Vue template snippet, what will be rendered if status is 'loading'?
<template>
<div>
<p v-if="status === 'success'">Success!</p>
<p v-else-if="status === 'loading'">Loading...</p>
<p v-else>Error occurred</p>
</div>
</template>Check which condition matches the status value.
The v-if checks if status is 'success', which is false. Then v-else-if checks if status is 'loading', which is true, so it renders 'Loading...'.
v-else in Vue?Choose the correct Vue template snippet that uses v-else properly.
v-else must immediately follow a v-if or v-else-if element.
Option A correctly places v-else immediately after v-if. Option A reverses the order, which is invalid. Option A mixes v-else and v-if on the same element, which is invalid. Option A uses an invalid attribute value for v-else.
Examine the Vue template below and identify the cause of the error.
<template>
<div>
<p v-if="isActive">Active</p>
<span>Not active</span>
<p v-else>Inactive</p>
</div>
</template>Check the placement of v-else relative to v-if.
Vue requires v-else to be immediately after the v-if or v-else-if element. Here, a span is between them, causing a compilation error.
count is 0?Consider this Vue template and data:
<template>
<div>
<p v-if="count > 0">Positive</p>
<p v-else-if="count === 0">Zero</p>
<p v-else>Negative</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>Check the conditions in order and see which matches count.
count is 0, so count > 0 is false, count === 0 is true, so 'Zero' is rendered.
v-else not have a condition?In Vue, v-else cannot have a condition like v-if or v-else-if. Why is this restriction important?
Think about the purpose of v-else as a fallback.
v-else is designed to show content only if all previous v-if and v-else-if conditions fail. Adding a condition would contradict its role as a fallback.