Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Vue 3 Conditional Statements

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:

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