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
<!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"> <template v-if="isDisplay"> <!--if...else... usetemplateImplement multiple elements--> <div>Show me1</div> <div>Show me11</div> <div>Show me12</div> <div>Show me13</div> </template> <div v-else> Show me2 </div> </div> <script type="text/javascript"> var myVue =new Vue({ el: ".test", data: { isDisplay: 1 } }) </script> </body> </html>
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
<!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-show="isDisplay"> <!--if...else... Single element,Pay attention v-show Not supported <template> grammar--> 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>
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.