1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>vue</title> </head> <body> <div id="root"> <div>{{mes}}--{{age}}</div> <ul v-show="flag" > <!-- v-show表示display属性表示控件是否显示--> <div>v-for遍历数组元素</div> <li v-for="(v,i) in lists">{{ v }}--{{ i }}</li> <div>遍历k vue</div> <li v-for="(v,key) in arr">{{ v }}--{{ key }}</li> <div>遍历数组列表元素</div> <li v-for="(v,i) in arrlist">{{ v }}--{{ i }}</li> <li v-for="(v,i) in arrlist">{{ v.name }}--{{ v.id }}</li> </ul> <button v-on:click="clike2">点击2</button><!--点击按钮2 age=25 flag=true(控件显示)显示呜呜呜--> <button v-on:click="clike1">点击1</button><!--点击按钮1 age=21 flag=false(控件消失) 显示哈哈哈--> <p v-if="age==19">嘿嘿嘿</p> <p v-else-if="age==21">哈哈哈</p> <p v-else>呜呜呜</p>
</div> </body> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script type="text/javascript"> //实例化一个Vue对象 new Vue({ el: "#root", data:{ mes:"hello", //显示内容 flag:true, age:19,//初始化年龄为19 lists: ['Jake','Boy','Alies'], arr:{name:"aa",id:0001,name:"bb",id:0002}, arrlist:[{name:"cccc",id:00003},{name:"dddd",id:00005},{name:"ffff",id:00004}]
}, methods:{ clike1:function () { this.age=21; this.mes="看我"; this.flag=false;
/* body... */ }, clike2:function(){ this.mes="再看我"; this.flag=true; this.age=25; }
} }); </script> </html>
|