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指令用来条件渲染一个块(block)元素.这个块元素只在指令表达式的结果为真值时候渲染.
<h1 v-if="awesome">Vue is awesome!</h1>
It is also possible to add an "else block" with v-else:
在v-if后面加一个v-else来渲染一个"else块元素"也是可行的:
<h1 v-if="awesome">Vue is awesome!</h1>
<h1 v-else>Oh no 😢</h1>
Conditional Groups with v-if on <template> 在<template>上使用v-if来条件显示一组元素
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是一个指令,所以必须添加到单个元素上. 但是如果我们想切换多个元素是否显示怎么办呢? 这种情况下,可以把v-if绑定到一个<template>元素上.这时候,<template>会被当作一个不可见的包裹元素(类似react的<> /<Fragment/>).最终渲染结果不会有<template>元素.
<template v-if="ok">
<h1>Title</h1>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</template>
v-else
You can use the v-else directive to indicate an "else block" for v-if:
可以使用v-else指令来表示一个v-if的"else块"
<div v-if="Math.random() > 0.5">
Now you see me
</div>
<div v-else>
Now you don't
</div>
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不是单独存在的元素)
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块元素",可以连续多次使用:
<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元素.
v-show
Another option for conditionally displaying an element is the v-show directive. The usage is largely the same:
另一个条件渲染元素是否显示的指令是v-show,使用和之前的大致相同:
<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指令只是切换元素上的CSSdisplay属性值.
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也是懒惰的lazy: 如果初始渲染条件为假,他什么都不会渲染——直到条件第一次变为真之前,条件块不会被渲染.
In comparison, v-show is much simpler - the element is always rendered regardless of initial condition, with CSS-based toggling.
相比之下,v-show就很简单——元素恒被渲染无论初始条件真伪,切换也只基于CSS.
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 with v-for 和v-for一起使用的v-if
::: tip Note
Using v-if and v-for together is not recommended. See the style guide for further information.
:::
提示 ⚠️
v-if和v-for一起使用是不建议的. 具体见 样式指南
When used together with v-if, v-for has a higher priority than v-if. See the list rendering guide for details.
当v-for和v-if一起使用的时候,v-for有更高的优先级. 细节见 列表渲染指南