Vue 3 Conditional Statements
In Vue 3, you can use various conditional statements in templates to control component rendering.
The main conditional statements are v-if, v-else-if, v-else and v-show; below are their usage and differences:
v-if: the element is not rendered into the DOM when the condition isfalse, suitable for situations where the condition changes infrequently.v-show: the element is always rendered into the DOM, suitable for situations where the condition changes frequently, with better show/hide toggle performance.v-else-ifandv-else: used to handle multiple conditional branches, used together withv-if.
Conditional Judgment
v-if
Conditionally renders an element based on the truthiness of an expression. If the expression is true, the element is rendered; if false, it is not (no such element is generated in the DOM).
Conditional judgment uses the v-if directive; the element is shown only when the directive's expression returns true:
v-if Directive
Use the v-if directive in an element:
<div id="app">
<p v-if="seen">Now you see me
p
div
<
script
const app = { data() { return { seen: true /* set to false, the message will not display */ } } } Vue.createApp(app).mount('#app')
script
Try it »
Here, the v-if directive will decide whether to insert the p element based on the value of the expression seen (true or false).
Because v-if is a directive, it must be added to an element. If there are multiple elements, you can wrap them in
and use v-if on it. The final render result will not include
.
in
Use the v-if directive on an element:
< div id = " app " const app = { data() { return { seen: true /* set to false, the message will not display */ } } } Vue.createApp(app).mount('#app')
script Try it » Used together with v-if, representing the element to render when the previous v-if expression is false. You can use the v-else directive to add an "else" block to v-if: Randomly generate a number, determine if it is greater than 0.5, then output the corresponding message: Math.random() > 0.5 " Random number greater than 0.5
div Random number less than or equal to 0.5
div
div Try it » Add extra conditional judgments between v-if and v-else; you can use multiple v-else-if in a row. Judge the value of the type variable: type === 'A' " A
div type === 'B' " B
div type === 'C' " C
div Not A/B/C
div
div < script const app = { data() { return { type: "C" } } } Vue.createApp(app).mount('#app')
script Try it » v-else and v-else-if must follow v-if or v-else-if. Conditionally shows or hides an element based on the truthiness of an expression. Unlike v-if, v-show always keeps the element in the DOM, controlling its visibility only through the CSS display property. We can also use the v-show directive to show elements based on a condition: Hello!
h1 Try it » If you need to frequently toggle an element's visibility, v-show is recommended, because it simply toggles CSS styles rather than re-rendering the entire element.v-if Directive
<
template
v-if
=
"
seen
"
<
h1
Website
h1
<
p
Google
p
<
p
Runoob
p
<
p
Taobao
p
template
div
<
scriptv-else
v-else Directive
<div id="app">
<div v-if="><
div
v-else<
script
Vue.createApp().mount('#app')
scriptv-else-if
v-else Directive
<div id="app">
<div v-if="><div v-else-if="><div v-else-if="><
div
v-else
v-show
v-show Directive
<h1 v-show="ok">