v-show directive in Vue
Another option for conditional display is the v-show
directive. It is used very similarly:
<template>
<p v-show="isAuth">
+++
</p>
</template>
The difference is that an element with v-show
will always be rendered and remain in the DOM, and only its CSS property display
will be toggled.
The v-if
directive performs "true" conditional rendering, as it ensures that event listeners and child components within the block are properly destroyed and recreated when the condition is toggled.
The v-if
directive is also lazy: if the condition is false at the time of the initial rendering, it will do nothing - the conditional block will not be rendered until the condition becomes true.
In comparison, v-show
is much simpler - the element is always rendered, regardless of the initial state, with CSS-based toggling.
In general, v-if
has higher switching costs, while v-show
has higher initial draw costs. So use v-show
if switching will be frequent, and prefer v-if
if the condition may not change during execution.
The v-show
directive cannot be used on a template
element and does not work with v-else
.
Given a paragraph and a button, make the paragraph toggle when the button is pressed.
Explain the differences between the v-show
and v-if
directives.
Tell me when it is better to use the v-show
directive, and when - v-if
.
Please tell me what limitations the v-show
directive has.