Guide: Conditional Rendering Flashcards
What element can we use to encapsulate multiple elements in a v-if
directive?
template
element, which is an invisible wrapper that won’t be rendered in the final result
What is one thing you must make sure of with a v-else-if
and v-else
directive?
They must immediately follow their appropriate directives, otherwise they won’t be recognized.
When toggling between elements in a if/else section, common elements being resued becomes a possibility. How do you combat this?
Use key="some-unique-value"
on both of the elements so that they won’t be reused and will properly be re-created.
What’s important ways that v-show
differs from v-if
?
-
v-show
simply toggles the CSS display property, the element is always rendered regardless of the condition -
v-show
can not be used on an actualtemplate
element, only on actual elements -
v-if
is “real” conditional rendering: ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles - If
v-if
is false on initial render, it will not do anything. Will only render once the condition becomes true for the first time - known as being lazy
What’s a good rule of thumb for when to use v-show
and v-if
?
- Use
v-show
if you need to toggle something very often - Use
v-if
if the condition is unlikely to change at runtime
Which has a higher precedence: v-if
or v-for
?
v-for
has a higher priority than v-if
.