Vue 3 v-show Directive
v-show The directive is used to toggle an element's visibility based on the truthiness of an expression. It controls the element's show/hide through the CSS display property.
Basic Explanation
v-show The directive decides whether the element is shown based on the truthiness of the bound expression. Whether the condition is true or false, the element is rendered into the DOM; only its visibility is controlled via CSS.
- Expected type: any
- Purpose: toggles an element's visibility based on the truthiness of an expression, controlled via the CSS display property.
Difference Between v-show and v-if
| Feature | v-show | v-if |
|---|---|---|
| Rendering Method | Always rendered into the DOM, only controlling show/hide | Rendered when the condition is true, not rendered when false |
| Toggle Performance | Low toggle cost (only changes CSS) | High toggle cost (needs to create/destroy elements) |
Use Cases
1. Frequently Toggle Visibility
When an element needs to show/hide frequently, using v-show gives better performance:
Example
Open modal
Vue 3 Built-in Directives