SoFunction
Updated on 2025-03-08

Simple understanding of Vue conditional rendering

1. v-if displays a single element

Note that else can only follow v-if or v-show

<!DOCTYPE html>
<html>
 <head>
 <meta charset="UTF-8">
 <title>VueConditional Rendering</title>
 <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
 <script src="../js/" type="text/javascript" charset="utf-8"></script>
 </head>
 <body>
 <div class="test">
 <div v-if="isDisplay"> <!--if...else... Single element-->
 Show me1
 </div>
 <div v-else>
 Show me2
 </div>
 </div>
 <script type="text/javascript">
 var myVue =new Vue({
 el: ".test",
 data: {
  isDisplay: 1
 }
 })
 </script>
 </body>
</html>

The output result is: Show me 1

2. v-if displays multiple elements, and needs to be combined with <template>

Note that else can only follow v-if or v-show

&lt;!DOCTYPE html&gt;
&lt;html&gt;
 &lt;head&gt;
 &lt;meta charset="UTF-8"&gt;
 &lt;title&gt;VueConditional Rendering&lt;/title&gt;
 &lt;meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /&gt;
 &lt;script src="../js/" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
 &lt;div class="test"&gt;
 &lt;template v-if="isDisplay"&gt; &lt;!--if...else... usetemplateImplement multiple elements--&gt;
 &lt;div&gt;Show me1&lt;/div&gt;
 &lt;div&gt;Show me11&lt;/div&gt;
 &lt;div&gt;Show me12&lt;/div&gt;
 &lt;div&gt;Show me13&lt;/div&gt;
 &lt;/template&gt;
 &lt;div v-else&gt;
 Show me2
 &lt;/div&gt;
 &lt;/div&gt;
 &lt;script type="text/javascript"&gt;
 var myVue =new Vue({
 el: ".test",
 data: {
  isDisplay: 1
 }
 })
 &lt;/script&gt;
 &lt;/body&gt;
&lt;/html&gt;

Output result: Show me 1 Show me 11 Show me 12 Show me 13

3. v-show only supports single element display, and does not support multiple elements contained in <template>

Note that else can only follow v-if or v-show

&lt;!DOCTYPE html&gt;
&lt;html&gt;
 &lt;head&gt;
 &lt;meta charset="UTF-8"&gt;
 &lt;title&gt;VueConditional Rendering&lt;/title&gt;
 &lt;meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" /&gt;
 &lt;script src="../js/" type="text/javascript" charset="utf-8"&gt;&lt;/script&gt;
 &lt;/head&gt;
 &lt;body&gt;
 &lt;div class="test"&gt;
 &lt;div v-show="isDisplay"&gt; &lt;!--if...else... Single element,Pay attention v-show Not supported &lt;template&gt; grammar--&gt;
 Show me1
 &lt;/div&gt;
 &lt;div v-else&gt;
 Show me2
 &lt;/div&gt;
 &lt;/div&gt;
 &lt;script type="text/javascript"&gt;
 var myVue =new Vue({
 el: ".test",
 data: {
  isDisplay: 1
 }
 })
 &lt;/script&gt;
 &lt;/body&gt;
&lt;/html&gt;

4. The difference between v-if and v-show

(1) v-if is real rendering and uninstalling, but after the first rendering, the result will be cached.

(2) The v-show element is always compiled and retained, just simply switched based on CSS

(3) Summary: If you need to switch frequently, it is better to change v-show, but if the conditions are unlikely to change at runtime, it is better to change v-if

gitHub address:/lily1010/vue_learn/tree/master/lesson08

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.