赞
踩
遍历数据渲染页面是非常常用的需求,Vue 中通过 v-for 指令来实现
语法:v-for=“item in items”
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>v-for</title> </head> <body> <div id="app"> <ul> <!-- v-for遍历建议使用:key唯一属性,加快vue渲染 --> <li v-for="p in users" :key="p.name"> {{p.name}}---{{p.gender}}---{{p.age}} </li> </ul> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script> new Vue({ el: "#app", data: { users: [ { name: '柳岩', gender: '女', age: 21 }, { name: '赵奕欢', gender: '女', age: 18 }, { name: '范冰冰', gender: '女', age: 24 }, { name: '刘亦菲', gender: '女', age: 18 }, { name: '古力娜扎', gender: '女', age: 25 }] } }) </script> </body> </html>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>v-for</title> </head> <body> <div id="app"> <ul> <!-- v-for遍历建议使用:key唯一属性,加快vue渲染 --> <li v-for="p in users" :key="p.name"> {{p.name}}---{{p.gender}}---{{p.age}} </li> <hr> <p style="font-size: larger;">遍历并显示当前索引值:</p> <li v-for="(p,index) in users"> 当前索引为{{index}}---{{p.name}}---{{p.sex}}---{{p.age}} </li><br><br> </ul> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script> new Vue({ el: "#app", data: { users: [ { name: '柳岩', gender: '女', age: 21 }, { name: '赵奕欢', gender: '女', age: 18 }, { name: '范冰冰', gender: '女', age: 24 }, { name: '刘亦菲', gender: '女', age: 18 }, { name: '古力娜扎', gender: '女', age: 25 }] } }) </script> </body> </html>
v-for 除了可以迭代数组,也可以迭代对象。语法基本类似
v-for=“value in object”
v-for="(value,key) in object"
v-for="(value,key,index) in object"
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>v-for</title> </head> <body> <div id="app"> <ul> <!-- v-for遍历建议使用:key唯一属性,加快vue渲染 --> <li v-for="p in users" :key="p.name"> {{p.name}}---{{p.gender}}---{{p.age}} <p style="font-size: small;">遍历对象信息:</p> <span v-for="user in p">{{user}},</span> </li> <hr> <p style="font-size: larger;">遍历并显示当前索引值:</p> <li v-for="(p,index) in users"> 当前索引为{{index}}---{{p.name}}---{{p.sex}}---{{p.age}} <p style="font-size: medium;">遍历对象信息显示键值:</p> <span v-for="(user,k) in p">{{k}}:{{user}} </span> <p style="font-size: larger;">遍历对象信息显示键值和索引:</p> <span v-for="(i,k,user) in p">{{user}} :{{k}} ==>{{i}} </span> </li><br><br> </ul> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script> new Vue({ el: "#app", data: { users: [ { name: '柳岩', gender: '女', age: 21 }, { name: '赵奕欢', gender: '女', age: 18 }, { name: '范冰冰', gender: '女', age: 24 }, { name: '刘亦菲', gender: '女', age: 18 }, { name: '古力娜扎', gender: '女', age: 25 }] } }) </script> </body> </html>
用来标识每一个元素的唯一特征,这样 Vue 可以使用“就地复用”策略有效的提高渲染的效率(加快vue渲染)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>v-for</title> </head> <body> <div id="app"> <ul> <!-- v-for遍历建议使用:key唯一属性,加快vue渲染 --> <li v-for="p in users" :key="p.name"> {{p.name}}---{{p.gender}}---{{p.age}} <p style="font-size: small;">遍历对象信息:</p> <span v-for="user in p">{{user}},</span> <hr> <p style="font-size: larger;">遍历并显示当前索引值:</p> <li v-for="(p,index) in users"> 当前索引为{{index}}---{{p.name}}---{{p.sex}}---{{p.age}} <p style="font-size: medium;">遍历对象信息显示键值:</p> <span v-for="(user,k) in p">{{k}}:{{user}} </span> <p style="font-size: larger;">遍历对象信息显示键值和索引:</p> <span v-for="(i,k,user) in p">{{user}} :{{k}} ==>{{i}} </span> </li><br><br> </ul> <p>遍历nums:</p> <ul> <li v-for="(num,index) in nums" :key="index">{{num}}</li> </ul> </div> <script src="./node_modules/vue/dist/vue.js"></script> <script> new Vue({ el: "#app", data: { users: [ { name: '柳岩', gender: '女', age: 21 }, { name: '赵奕欢', gender: '女', age: 18 }, { name: '范冰冰', gender: '女', age: 24 }, { name: '刘亦菲', gender: '女', age: 18 }, { name: '古力娜扎', gender: '女', age: 25 }], nums: [1, 2, 3, 4, 4] } }) </script> </body> </html>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。