v-for實現迴圈巢狀
阿新 • • 發佈:2019-01-09
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <title></title> 6 <meta charset="UTF-8"> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <script src="./js/vue2.js"></script> 9 </head> 10 11 <body> 12 <div id="app"> 13 <ul> 14 <!-- 先從origin獲得對應的資料 --> 15 <li v-for="(value,index,key) in origin"> 16 {{value.info}} 17 <!-- 在根據從origin獲得的資料遍歷對應的物件 --> 18 <ul> 19 <li v-for="items in value.project"> 20 {{items}} 21 </li> 22 </ul> 23 </li> 24 </ul> 25 </div> 26 <script> 27 var vm = new Vue({ 28 el: '#app', 29 data: {30 origin:[ 31 { 32 info:"all", 33 "project":{"name":"wikina","age":"20","birthday":"1996"}, 34 }, 35 { 36 info:"all", 37 "project":{"name":"wikina","age":"20","birthda":"1996"}, 38 }, 39 { 40 info:"all", 41 "project":{"name":"wikina","age":"20","birthda":"1996"}, 42 } 43 ] 44 45 } 46 }) 47 </script> 48 </body> 49 50 </html>
1 <!DOCTYPE html> 2 <html lang="en"> 3 4 <head> 5 <title></title> 6 <meta charset="UTF-8"> 7 <meta name="viewport" content="width=device-width, initial-scale=1"> 8 <script src="./js/vue2.js"></script> 9 </head> 10 11 <body> 12 <div id="app"> 13 <ul> 14 <!-- 先從origin獲得對應的資料 --> 15 <li v-for="(value,index,key) in origin"> 16 {{value.info}} 17 <!-- 在根據從origin獲得的資料遍歷對應的物件 --> 18 <ul> 19 <li v-for="items in value.project"> 20 {{items}} 21 </li> 22 </ul> 23 </li> 24 </ul> 25 </div> 26 <script> 27 var vm = new Vue({ 28 el: '#app', 29 data: { 30 origin:[ 31 { 32 info:"all", 33 "project":{"name":"wikina","age":"20","birthday":"1996"}, 34 }, 35 { 36 info:"all", 37 "project":{"name":"wikina","age":"20","birthda":"1996"}, 38 }, 39 { 40 info:"all", 41 "project":{"name":"wikina","age":"20","birthda":"1996"}, 42 } 43 ] 44 45 } 46 }) 47 </script> 48 </body> 49 50 </html>View Code
這個原理很簡單,先使用v-for迴圈外層資料,然後再次迴圈就可以了,這個看程式碼就可以知道了,就不囉嗦了