v-if vs v-show
Why are they different and when to use them?
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
Usev-show
if you need to toggle something very often. Usev-if
if you need to toggle something more conditionally.
Happy Vue-ing! 🎇
Made with❤by leovoon