Vue v-show

Vue v-show Directive

Another directive used for conditionally displaying an element is v-show.

The usage is nearly identical to v-if, but the way it operates behind the scenes is completely different.

Understanding this difference is crucial for building high-performance Vue applications.


How v-show Works

Unlike v-if, an element with v-show will always be rendered and remain in the DOM.

When the condition becomes false, Vue simply applies the CSS property display: none to hide it.

When it becomes true, Vue removes the CSS property, making it visible again.

v-show Example:

<div id="app">
  <h1 v-show="isWarningVisible">Warning: Connection Lost!</h1>
</div>

v-if vs v-show (Performance)

You should choose between v-if and v-show based on how often your element changes state.

v-if has higher "toggle costs". Every time it turns on or off, Vue has to completely create or destroy the DOM element.

v-show has higher "initial render costs". The browser must build the element on page load, even if it is hidden instantly.

The Golden Rule:


No Else Blocks for v-show

It's important to remember that v-show does not support v-else or v-else-if.

If you need complex branching logic, you must use the v-if family instead.


Exercise 1 of 2

?

How does v-show hide an element from the user?

Exercise 2 of 2

?

If you have a modal popup that the user will open and close dozens of times, which directive is more performant?