Conditional rendering
Table of contents
- ##v-if vs v-show
- v-if used with v-for
# 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
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
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:
v-else<div v-if="Math.random() > 0.5">
Now you see me
</div>
<div v-else>
Now you don't
</div>
v-if
orv-else- if after the element, otherwise it will not be recognized.
##2.1.0 Newv-else-if
v-ifis similar toand 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>
v-else,
v-else-if must also immediately follow the element with
v-if
v-else-if after.
Use
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: Then switching Try it yourself, enter some text in the input box and press the toggle button: 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 Now the input box will be re-rendered every time it switches. See: <label> directive. The usage is roughly the same: <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>
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. 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>
elements are still efficiently reused because they do not have the
key added Attributes.
Another one is used according to The option for conditionally displaying elements is the ##v-show
v-show<h1 v-show="ok">Hello!</h1>
The difference is that elements with
will always be rendered and remain in the DOM. v-show
simply switches the CSS property display
of the element.
v-showdoes not support the
<template>
element, nor does it supportv-else
.
##v-if vs
v -show
v-ifis "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
In contrast,
v-show
Generally speaking,
v-if
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
andv-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.