Logical rendering uses directives to control conditional display and list iteration in templates.
Conditional rendering
v-show
The v-show directive toggles the visibility of a node. The node is always rendered, regardless of whether the v-show expression evaluates to true or false.
-
When
v-showevaluates totrue,display: flexis added to the node's CSS style. -
When
v-showevaluates tofalse,display: noneis added to the node's CSS style.
<div class="div" v-show="exist(exist4)"></div>
v-if
The v-if directive conditionally renders a block of content. The block is rendered only when the expression evaluates to true.
-
v-else-ifserves as an "else-if block" forv-if. Multiplev-else-ifblocks can be chained. -
v-elseserves as an "else block" 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>
-
A
v-else-ifblock must immediately follow av-ifblock. Otherwise, it will not be recognized. -
A
v-elseblock must immediately follow av-iforv-else-ifblock. Otherwise, it will not be recognized. -
Do not use
v-ifon the root node of a template.
List rendering
v-for
The v-for directive renders a block of content repeatedly based on an array, a number, or an object. Six syntaxes 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>
-
The
v-fordirective does not support nesting. Do not use av-fordirective inside anotherv-forrendering block. -
When rendering data based on an object, the display order of the items is not guaranteed. To display items in a specific order, traverse an array or
numberinstead. -
Do not use
v-foron the root node of a template.
Sample code
Click detailLogicalRender.zip to download the complete sample code.