Difference between v-if and v-show in Vue
v-if
- event listeners and child components inside the conditional block are destroyed and re-created during toggles.
- can group on
<template>
. The rendered result will not include the<template>
element.
<template v-if="isShow">
<div>
<p>Hello</p>
</div>
<button>Say Hi</button>
</template>
- can use together
v-else-if
andv-else
v-show
- always rendered and remains in the DOM regardless of initial condition
- only toggles the display CSS property of the element.
- does not support
template
andv-else
<template>
<h1 v-show="isShow">Hello world</h1>
</template>
In short
Use v-show
if you need to toggle something very often.
Use v-if
if you need to toggle something more conditionally.
Happy Vue-ing!
You may also like:
- Motion Library for Vue - Lunar New Year Red Packet
- Easy Swipe Carousel in Vue
- Exploring TypeScript in Vue
- Recreating sticky scrolling animation by Remix
- Creating Before and After Image Comparison Slider with Vue 3
- Building Accordion Component in Vue 3
- Lazy Load Components in Vue 3 with Suspense
- Creating a ATM-liked Input Component in Vue 3