条件渲染 | Conditional Rendering
v-if
The directive v-if
is used to conditionally render a block. The block will only be rendered if the directive's expression returns a truthy value.
v-if
指令用于条件性地渲染一块内容。这块内容只会在指令的表达式返回真值时才被渲染。
template
<h1 v-if="awesome">Vue is awesome!</h1>
v-else
You can use the v-else
directive to indicate an "else block" for v-if
:
你也可以使用 v-else
为 v-if
添加一个“else 区块”。
template
<button @click="awesome = !awesome">Toggle</button>
<h1 v-if="awesome">Vue is awesome!</h1>
<h1 v-else>Oh no 😢</h1>
Vue is awesome!
A v-else
element must immediately follow a v-if
or a v-else-if
element - otherwise it will not be recognized.
一个 v-else
元素必须跟在一个 v-if
或者 v-else-if
元素后面,否则它将不会被识别。
v-else-if
The v-else-if
, as the name suggests, serves as an "else if block" for v-if
. It can also be chained multiple times:
顾名思义,v-else-if
提供的是相应于 v-if
的“else if 区块”。它可以连续多次重复使用:
template
<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>
Similar to v-else
, a v-else-if
element must immediately follow a v-if
or a v-else-if
element.
和 v-else
类似,一个使用 v-else-if
的元素必须紧跟在一个 v-if
或一个 v-else-if
元素后面。
<template>
上的 v-if
| v-if
on <template>
Because v-if
is a directive, it has to be attached to a single element. But what if we want to toggle more than one element? In this case we can use v-if
on a <template>
element, which serves as an invisible wrapper. The final rendered result will not include the <template>
element.
因为 v-if
是一个指令,他必须依附于某个元素。但如果我们想要切换不止一个元素呢?在这种情况下我们可以在一个 <template>
元素上使用 v-if
,这只是一个不可见的包装器元素,最后渲染的结果并不会包含这个 <template>
元素。
template
<template v-if="ok">
<h1>Title</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</template>
v-else
and v-else-if
can also be used on <template>
.
v-else
和 v-else-if
也可以在 <template>
上使用。
v-show
Another option for conditionally displaying an element is the v-show
directive. The usage is largely the same:
另一个可以用来按条件显示一个元素的指令是 v-show
。其用法基本一样:
template
<h1 v-show="ok">Hello!</h1>
The difference is that an element with v-show
will always be rendered and remain in the DOM; v-show
only toggles the display
CSS property of the element.
不同之处在于 v-show
会在 DOM 渲染中保留该元素;v-show
仅切换了该元素上名为 display
的 CSS 属性。
v-show
doesn't support the <template>
element, nor does it work with v-else
.
v-show
不支持在 <template>
元素上使用,也不能和 v-else
搭配使用。
v-if
vs. v-show
v-if
is "real" conditional rendering because it ensures that event listeners and child components inside the conditional block are properly destroyed and re-created during toggles.
v-if
是“真实的”按条件渲染,因为它确保了在切换时,条件区块内的事件监听器和子组件都会被销毁与重建。
v-if
is also lazy: if the condition is false on initial render, it will not do anything - the conditional block won't be rendered until the condition becomes true for the first time.
v-if
也是惰性的:如果在初次渲染时条件值为 false,则不会做任何事。条件区块只有当条件首次变为 true 时才被渲染。
In comparison, v-show
is much simpler - the element is always rendered regardless of initial condition, with CSS-based toggling.
相比之下,v-show
简单许多,元素无论初始条件如何,始终会被渲染,只有 CSS display
属性会被切换。
Generally speaking, v-if
has higher toggle costs while v-show
has higher initial render costs. So prefer v-show
if you need to toggle something very often, and prefer v-if
if the condition is unlikely to change at runtime.
总的来说,v-if
有更高的切换开销,而 v-show
有更高的初始渲染开销。因此,如果需要频繁切换,则使用 v-show
较好;如果在运行时绑定条件很少改变,则 v-if
会更合适。
v-if
和 v-for
警告
It's not recommended to use v-if
and v-for
on the same element due to implicit precedence. Refer to style guide for details.
同时使用 v-if
和 v-for
是不推荐的,因为这样二者的优先级不明显。请查看风格指南获得更多信息。
When v-if
and v-for
are both used on the same element, v-if
will be evaluated first. See the list rendering guide for details.
当 v-if
和 v-for
同时存在于一个元素上的时候,v-if
会首先被执行。请查看列表渲染指南获取更多细节。