Skip to Content

How To Use V-Show Vue With Vue Display Examples

If you’ve ever wanted to show or hide elements based on certain conditions in your Vue.js applications, v-show Vue is the directive you need to master.

By using v-show Vue, you can effortlessly control the visibility of elements based on boolean expressions, giving you the flexibility to dynamically adjust your UI. 

In this comprehensive tutorial, we will take a hands-on approach to explore various examples of using v-show in Vue.js.

By the end of this tutorial, you’ll be equipped with the knowledge and skills to leverage v-show effectively and create engaging user interfaces.

By the end of this guide, you will have a solid understanding of how to leverage v-show to create dynamic and interactive user interfaces in Vue.js.

No products found.

Learn more by reading my longer article about Implement Vue Hover With Mouseover Examples

V-Show Vue

V-Show Vue is a directive used for conditional rendering. It allows you to control the visibility of elements based on a boolean value.

What Is V-Show Vue?

In Vue.js, v-show is a directive used for conditional rendering. It allows you to control the visibility of elements based on a boolean value.

The main difference between v-show and v-if is that v-show toggles the CSS display property of the element to show or hide it, while v-if completely adds or removes the element from the DOM.

When the expression assigned to v-show evaluates to true, the element is displayed by setting its CSS display property to its original value (e.g., block, inline, etc.).

Conversely, when the expression evaluates to false, the element is hidden by setting its display property to none.

Unlike v-if, which inserts or removes elements from the DOM, v-show keeps the element in the DOM at all times.

This means that even if the element is hidden, it still occupies space and retains its state.

V-Show Vue 3

V-Show Vue 3: Here’s an example of using v-show in Vue 3:

V-Show Vue 3

In this example, we have a button and a paragraph element. The isVisible data property is initially set to false, so the paragraph is hidden.

When the button is clicked, the toggleVisibility method is called, which toggles the value of isVisible.

This, in turn, shows or hides the paragraph based on the v-show directive. If isVisible is true, the paragraph is displayed; if it’s false, the paragraph is hidden.

V-Show Vue 2

V-Show Vue 2: The usage of v-show in Vue 2 is similar to Vue 3. Here’s an example:

V-Show Vue 2

This example demonstrates the same functionality as the Vue 3 example. The paragraph is hidden by default and can be toggled to show or hide by clicking the button.

The v-show directive controls the visibility of the paragraph based on the value of isVisible.

V-Show Vue Example

V-Show Vue Example: Let’s take a closer look at a simple example of using v-show in Vue:

V-Show Vue Example

In this example, we have a button that triggers the toggleVisibility method when clicked. The isVisible data property is initially set to false, so the paragraph with v-show is hidden.

Clicking the button toggles the value of isVisible, resulting in the paragraph being shown or hidden accordingly.

Check out my article about Learn Migrating Vue 2 To Vue 3 With Vue Migration Examples.

V-Show Transition Vue

V-Show Transition Vue: If you want to add transition effects to elements controlled by v-show, you can make use of Vue’s built-in transition system. Here’s an example:

V-Show Transition Vue

In this example, we have wrapped the paragraph element inside a <transition> component with the name attribute set to “fade”.

This enables a fade-in and fade-out transition effect when the visibility of the paragraph changes. The CSS styles define the animation properties for the fade effect.

V-Show Else Vue

V-Show Else Vue: By default, Vue’s v-show directive does not provide a built-in “else” behavior.

If you need to conditionally show different elements based on a boolean value, it’s recommended to use v-if instead.

However, there is a workaround to achieve a similar effect using v-show and a computed property.

V-Show Else Vue

In this example, we have two paragraphs controlled by v-show. The first paragraph is displayed when isVisible is true, and the second paragraph is displayed when isVisible is false.

By toggling the isVisible value, the visibility of the paragraphs alternates, giving the impression of an “else” behavior.

Don´t forget to check out my article about Learn Vue Router Hash With Vue Router Examples

V-Show vs V-if

What Is The Difference Between Show And VIF?

The primary difference between v-show and v-if lies in how they handle elements in the DOM.

v-show toggles the visibility of an element by manipulating its CSS display property.

When the expression assigned to v-show evaluates to true, the element is displayed by setting display: <original-value>, and when it evaluates to false, the element is hidden by setting display: none.

Importantly, the element remains in the DOM at all times, even when hidden.

On the other hand, v-if completely inserts or removes elements from the DOM based on the truthiness of the expression.

When the condition in v-if evaluates to true, the element is added to the DOM. Conversely, if the condition becomes false, the element is removed from the DOM.

V-show vs V-if Performance

V-show vs V-if Performance: When it comes to performance, v-show has a slight advantage during initial rendering.

Since elements controlled by v-show are always present in the DOM, the initial rendering is faster compared to v-if, which adds or removes elements.

However, in scenarios where the condition is frequently toggled, v-show may have worse performance compared to v-if.

When v-show condition becomes false, the element is hidden using CSS, but it still occupies space in the DOM.

This can impact performance in situations where there are a large number of hidden elements.

In such cases, v-if can be more efficient as it completely removes elements from the DOM when the condition is false.

V-show And V-if Difference

V-show And V-if Difference: Here’s an example that demonstrates the difference between v-show and v-if:

V-show And V-if Difference

In this example, we have two paragraphs controlled by v-show and v-if, respectively. When the button is clicked, the toggleVisibility method is called, toggling the value of isVisible.

With v-show, the paragraph is always in the DOM but hidden or shown based on the value of isVisible.

With v-if, the paragraph is added to or removed from the DOM based on the condition.

V-if And V-show On Same Element

V-if And V-show On Same Element: It is generally not recommended to use both v-if and v-show on the same element.

If both directives are present, v-if takes precedence and will completely remove the element from the DOM if the condition is false, regardless of the v-show value.

Here’s an example illustrating the behavior:

V-if And V-show On Same Element

In this example, even if isVisible is true, the paragraph will not be displayed because v-if takes precedence and removes the element when the condition is false.

If you need to conditionally show or hide an element, it’s best to choose either v-if or v-show based on your specific requirements.

Don´t forget to check out my article about Add Vue Drag And Drop With Vue Draggable Examples.

What Is The Difference Between V For And V-show?

While both directives are used to manipulate the visibility and rendering of elements, they serve different purposes and have distinct use cases.

In this article, we will delve into the differences between v-for and v-show in Vue.js.

The Purpose of v-for:

The v-for directive is primarily used for rendering lists of elements or iterating over data arrays in Vue.js.

It allows you to loop through an array or an object’s properties and generate dynamic content based on the data.

With v-for, you can dynamically generate HTML elements, components, or even render computed values.

Here’s an example usage of v-for to render a list of items:

What Is The Difference Between V For And V-show? 1

In this example, the v-for directive iterates over an array called items and generates a list of <li> elements for each item in the array.

The :key attribute is used to provide a unique identifier for each rendered item, helping Vue.js efficiently update and manage the list.

The Purpose of v-show:

On the other hand, the v-show directive is used to conditionally show or hide elements based on a boolean expression.

It toggles the visibility of an element by manipulating the CSS display property.

Unlike v-for, v-show does not generate or manipulate elements dynamically. Instead, it controls the visibility of existing elements in the DOM.

Here’s an example usage of v-show to conditionally show an element:

What Is The Difference Between V For And V-show? 2

In this example, the v-show directive binds to a boolean variable isVisible. If isVisible evaluates to true, the paragraph element will be displayed; otherwise, it will be hidden.

Performance Considerations:

Since v-for dynamically creates elements based on the provided data, it has a higher computational cost compared to v-show.

When using v-for, Vue.js needs to perform additional work to track and update the rendered elements.

Therefore, it is recommended to use v-for when you need to render dynamic lists or generate elements based on data.

On the other hand, v-show toggles the visibility of existing elements without modifying the DOM structure.

It is more lightweight and performs better when the elements need to be shown or hidden frequently.

Use Cases:

Use v-for when you have an array or an object and need to generate elements dynamically based on the data.

It is suitable for rendering lists, tables, or any scenario that requires iterating over data.

Use v-show when you want to conditionally show or hide elements based on boolean expressions.

It is useful for displaying or hiding components, tooltips, modals, or any other elements that need to change visibility based on certain conditions.

Hopefully, you now have a better understanding of how you can use v-show Vue.

Understanding how you can use v-show Vue will help you to control the visibility of elements based on a boolean value.