This article describes the types of logical rendering and the corresponding instructions.
Conditional rendering
v-show
v-show
instructions are used to conditionally render the content of a node. The node is rendered regardless of whether the result of the v-show
instruction is true
or false
.
If the
v-show
result istrue
, it is equivalent to adding adisplay: flex
to the CSS style of the node.If the
v-show
result isfalse
, it is equivalent to adding adisplay: none
to the CSS style of the node.
<div class="div" v-show="exist(exist4)"></div>
v-if
v-if
instructions are used to conditionally render a piece of content. This piece of content will only be rendered when the expression of the directive returns a true
.
You can use
v-else-if
to act asv-if
"else-if blocks" that can be used consecutively.You can use
v-else
to act as "else blocks" forv-if
.
<div class="div" v-if="exist(a)">
...
</div>
<div class="div" v-else-if="exist(b)">
...
</div>
<div class="div" v-else>
...
</div>
The
v-else-if
must immediately follow thev-if
block or it will not be recognized.The
v-else
must immediately follow thev-if
orv-else-if
block or it will not be recognized.v-if
cannot be written on the root node of a template.
List rendering
vfor
v-for
instructions may render a set of data based on an array /number/
object. Currently, six methods are supported:
v-for="index in 10"
v-for="index in number"
v-for="item in array"
v-for="(item,index) in array"
v-for="item in object"
v-for="(item,key) in object"
<div class="vforStyle" v-for="(value,index) in obj">
<text class="content">{{index + value}}</text>
</div>
v-for
instructions do not support nested use, that is,v-for
can no longer usev-for
for rendering.When rendering data based on objects, the order in which objects are displayed is not controllable. To display in a determined order, iterate through the
array/number
data.v-for
cannot be written on the root node of a template.
Examples
Click here detailLogicalRender.zip for the complete sample code.