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
| <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>v-for遍历数组(列表)、对象(字典)</title> <script src="https://cdn.bootcdn.net/ajax/libs/vue/2.6.12/vue.min.js"></script> <style> table, td { border: 1px solid black; text-align: center; } </style> </head> <body>
<div id="box"> <h2>数组(列表)for循环遍历</h2> <ul> <li v-for="(value,index) in list_test">{{index}} —— {{value}}</li> </ul>
<h2>对象(字典)for循环遍历</h2> <ul> <li v-for="(value,key) in dic_test">{{key}} —— {{value}}</li> </ul>
<h2>数组(列表)套对象(字典)for循环遍历</h2> <table> <tr> <td>姓名</td> <td>年龄</td> <td>性别</td> <td>国籍</td> </tr> <tr v-for="info in summary_test"> <td>{{info.name}}</td> <td>{{info.age}}</td> <td>{{info.gender}}</td> <td>{{info.country}}</td> </tr> </table> </div> </body> <script> let vm = new Vue({ el: '#box', data: { list_test: ['First', 'second', 'Third', 'Forth', 'Fifth'], dic_test:{name: 'Darker', age: 18, gender: 'male'}, summary_test: [ {name: 'Alan', age: 23, gender: 'male', country: 'America'}, {name: 'Ben', age: 15, gender: 'male', country: 'Australia'}, {name: 'Cindy', age: 12, gender: 'female', country: 'Japan'}, {name: 'Darker', age: 18, gender: 'male', country: 'China'}, {name: 'Elisa', age: 26, gender: 'female', country: 'Mexico'}, ] } }) </script> </html>
|