Conditional rendering


Table of contents


# The

#v-if##v-if

directive is used to conditionally render a block of content. This content will only be rendered if the directive's expression returns a truthy value.
<h1 v-if="awesome">Vue is awesome!</h1>

You can also add an "else block" using v-else:

<h1 v-if="awesome">Vue is awesome!</h1>
<h1 v-else>Oh no ??</h1>


at

<template>

Use v-if conditional rendering grouping on elementsBecause v-if is a directive, So it must be added to an element. But what if you want to switch multiple elements? At this time, you can treat a

<template>

element as an invisible wrapping element and use v-if on it. The final rendering result will not contain the <template> element.

<template v-if="ok">
  <h1>Title</h1>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
</template>


##v-else

You can use the v-else command To represent the "else block" of v-if:

<div v-if="Math.random() > 0.5">
  Now you see me
</div>
<div v-else>
  Now you don't
</div>
v-else

The element must be immediately followed by

v-if

or

v-else- if after the element, otherwise it will not be recognized.

##v-else-if


##2.1.0 Newv-else-if

, as the name suggests, acts as the "else-if block" of
v-if

and can be used continuously:

<div v-if="type === 'A'">
  A
</div>
<div v-else-if="type === 'B'">
  B
</div>
<div v-else-if="type === 'C'">
  C
</div>
<div v-else>
  Not A/B/C
</div>
is similar to

v-else, v-else-if must also immediately follow the element with v-if

or

v-else-if after. Use

key

to manage reusable elements

Vue will do its best May render elements efficiently, often by reusing existing elements rather than rendering them from scratch. In addition to making Vue very fast, this has several other benefits. For example, if you allow users to switch between different login methods:

<template v-if="loginType === 'username'">
  <label>Username</label>
  <input placeholder="Enter your username">
</template>
<template v-else>
  <label>Email</label>
  <input placeholder="Enter your email address">
</template>

Then switching loginType in the above code will not clear what the user has entered. Because both templates use the same element, <input> is not replaced—only its placeholder is replaced.


Try it yourself, enter some text in the input box and press the toggle button:

1.gif

This doesn't always meet actual needs, so Vue provides you with a way to express "these two elements are completely independent, don't reuse them." Just add a key attribute with a unique value:

<template v-if="loginType === 'username'">
  <label>Username</label>
  <input placeholder="Enter your username" key="username-input">
</template>
<template v-else>
  <label>Email</label>
  <input placeholder="Enter your email address" key="email-input">
</template>

Now the input box will be re-rendered every time it switches. See:

2.gif

##Note that the

<label> elements are still efficiently reused because they do not have the key added Attributes.


##v-show

Another one is used according to The option for conditionally displaying elements is the
v-show

directive. The usage is roughly the same:

<h1 v-show="ok">Hello!</h1>

The difference is that elements with

v-show

will always be rendered and remain in the DOM. v-show simply switches the CSS property display of the element.

Note that
v-show

does not support the <template> element, nor does it support v-else.


##v-if vs v -show

v-if
is "true" conditional rendering, as it will ensure that events inside the conditional block are processed during the switch Listeners and subcomponents are destroyed and recreated appropriately.


v-if

is also lazy: if the condition is false on the initial render, nothing is done - not until the first time the condition becomes true. Start rendering the conditional block.

In contrast, v-show

is much simpler - the element will always be rendered regardless of the initial conditions, and is simply toggled based on CSS.

Generally speaking, v-if

has higher switching overhead, while

v-show has higher initial rendering overhead. Therefore, if you need to switch very frequently, it is better to use v-show; if the conditions rarely change at runtime, it is better to use v-if.

v-if Used with v-for


It is not recommended to use v-if and v-for at the same time. Please consult the Style Guide for more information.

When v-if is used with v-for, v-for has better performance than v-if Higher priority. Please consult the List Rendering Guide for details.