一 :

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 <body> 8 <div id="content"> 9 <div v-text="msg"></div> 10 <div v-html="msg"></div> 11 </div> 12 13 <script src="../vue.js"></script> 14 <script> 15 new Vue({ 16 el:'#content', 17 data(){ 18 return { 19 msg:'<h2>liang</h2>' 20 } 21 } 22 }) 23 </script> 24 </body> 25 </html>
二 ,
v-if vs v-show
v-if 是“真正”的条件渲染,因为它会确保在切换过程中条件块内的事件监听器和子组件适当地被销毁和重建。
v-if 也是惰性的:如果在初始渲染时条件为假,则什么也不做——直到条件第一次变为真时,才会开始渲染条件块。
相比之下,v-show 就简单得多——不管初始条件是什么,元素总是会被渲染,并且只是简单地基于 CSS 进行切换。
一般来说,v-if 有更高的切换开销,而 v-show 有更高的初始渲染开销。因此,如果需要非常频繁地切换,则使用 v-show 较好;如果在运行时条件很少改变,则使用 v-if 较好。

1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Title</title> 6 <style> 7 .box { 8 width: 200px; 9 height: 200px; 10 background-color: red; 11 } 12 13 .box2 { 14 width: 200px; 15 height: 200px; 16 background-color: green; 17 } 18 </style> 19 </head> 20 <body> 21 <div id="content"> 22 {{ add(2,3) }} 23 <!--给button按钮设置点击事件--> 24 <buttom v-on:click="handlerClick">隐藏</buttom> 25 <div class="box" v-show="isShow"></div> 26 <div class="box2" v-if="isShow"></div> 27 <div v-if="Math.random() > 0.5">有了</div> 28 <div v-else>没了</div> 29 </div> 30 31 <script src="../vue.js"></script> 32 <script> 33 new Vue({ 34 el:'#content', 35 data(){ 36 return { 37 msg:"<h2>liang</h2>", 38 num:1, 39 isShow:true 40 } 41 }, 42 methods:{ 43 add(x,y){ 44 console.log(this.num); 45 return x+y 46 }, 47 //如果目前是显示的,点击后隐藏,如果目前隐藏的,点击后显示 48 handlerClick(){ 49 console.log(this); 50 this.isShow = !this.isShow 51 } 52 } 53 }) 54 </script> 55 </body> 56 </html>
三 ,
v-bind可以绑定标签中任何属性
v-on 可以监听 js中所有事件
简写:
v-bind:src 等价于 :src
v-on:click 等价于 @click

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box { width: 200px; height: 200px; background-color: red; } .active{ background-color: green; } </style> </head> <body> <div id="app"> <button @mouseenter = "handlerChange" @mouseleave="handlerLeave">切换颜色</button> <img :src="img" :alt="msg"> <div class="box" :clas = '{active:isavtive}'></div> </div> <script src="../vue.js"></script> <script> new Vue({ el:'#app', data(){ return { img:'./1.jpg', msg:'美女', isactive:true } }, methods:{ handlerChange(){ this.isactive = false; }, handlerLeave(){ this.isactive = true } } }) </script> </body> </html>
四 , v-for遍历
v-for可以遍历列表,也可以遍历对象
在使用vue的v-for指令的时候,一定要绑定key,避免vue帮咱们计算DOM

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .box { width: 200px; height: 200px; background-color: red; } .active { background-color: green; } </style> </head> <body> <div id="app"> <ul v-if="data.status === 'ok'"> <li v-for = '(item,index) in data.users' :key="item.id"> <h3>{{ item.id }} -- {{ item.name }} -- {{ item.age }}</h3> </li> </ul> <div v-for = '(value,key) in person'> {{ key }} ---- {{ value }} </div> </div> <script src="../vue.js"></script> <script> new Vue({ el:'#app', data(){ return { data:{ status:'ok', users:[ {id:1,name:'liang',age:18}, {id:2,name:'ding',age:20}, {id:3,name:'sun',age:16}, ] }, person:{ name:'liang' } } }, methods:{} }) </script> </body> </html>