The v-show Directive
The v-show directive shows or hides an element without removing it. Vue keeps the element in the page and flips its display style.
Show the text
visible is true, so the paragraph shows.
<p v-show="visible">Hello, Vue!</p>Hide the text
With visible set to false, the same line hides the paragraph.
<p v-show="visible">Hello, Vue!</p>v-if removes instead
v-if removes the paragraph when visible is false.
<p v-if="visible">Hello, Vue!</p>Example
<script setup>
import { ref } from 'vue'
const visible = ref(true)
</script>
<template>
<div>
<p v-show="visible">Hello, Vue!</p>
<button @click="visible = !visible">Toggle</button>
</div>
</template>
Key points
- v-show takes a true or false value.
- A true value shows the element as normal.
- A false value hides the element with a display style of none.
- Display none is a style that hides an element while it stays in the page.
- v-if removes the element from the page when its condition is false.
- v-show suits an element that shows and hides often.
In this example, visible is a ref with the value true. The paragraph carries v-show with visible. The button flips visible each time it is clicked. The first render shows the paragraph. A verified variant with visible set to false hides the paragraph. Its display style is none, while the paragraph stays in the page.
Note
v-show does not work on a template tag, because that tag draws no element of its own.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does v-show do when its value is false?