<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <script src="js/vue.js"></script> <style> .div{ width: 100px; height: 100px; background: green; } </style> </head> <body> <div id="app"> <h2 v-if="hot">文章</h2> <!-- <h2>姚笛</h2> --> <h2 v-else>马伊琍</h2> <template v-if="hot"> <ul> <li>王宝强与马蓉</li> <li>林丹与谢杏芳</li> <li>陈羽凡与白百何</li> <li>潘粤明与董洁</li> <li>谢霆锋与张柏芝</li> </ul> </template> <template v-if="loginType"> <label for="">用户名:</label> <input type="text" placeholder="请输入您的用户名"> </template> <template v-else> <label for="">邮箱:</label> <input type="text" placeholder="请输入您的邮箱"> </template> <button @click="changeLogin">切换登录方式</button> <br> <br> <button @click="toggle">显示隐藏</button> <div class="div" v-show="show"></div> </div> <script> new Vue({ el:'#app', data:{ hot:true, loginType:true, show:true, }, methods:{ changeLogin(){ this.loginType=!this.loginType; }, toggle(){ this.show=!this.show; } }, }) /* v-if与v-show的区别 1、对template的支持 v-show不支持,v-if支持 2、元素隐藏的区别 v-if不会渲染元素,v-show会渲染 如何选择 1、如果从渲染的角度来说,v-if要比v-show性能好,如果条件很少变化就要用v-if 2、如果从显示与隐藏的角度来说,v-show要比v-if性能好。如果要频繁的切换条件,就要使用v-show */ </script> </body> </html>