vue中迴圈遍歷使用的指令是v-for 1.v-for遍曆數組 (1)value in arr 遍曆數組中的元素 (2)(value,index) in arr 遍曆數組中的元素和數組下標 運行代碼: 輸出結果: 2.v-for遍歷json對象 (1)value in json 遍歷json對象中的 ...
vue中迴圈遍歷使用的指令是v-for
1.v-for遍曆數組
(1)value in arr 遍曆數組中的元素
(2)(value,index) in arr 遍曆數組中的元素和數組下標
運行代碼:
<body> <div class="box"> <ul> <li v-for="value in arr">{{value}}</li><br> <li v-for="(value,index) in arr">{{value}}--{{index}}</li> </ul> </div> <script src="js/vue.js"></script> <script> new Vue({ el:".box", data:{ arr:["哈哈","嘻嘻","哼哼"] } }); </script> </body>
輸出結果:
2.v-for遍歷json對象
(1)value in json 遍歷json對象中的值
(2)(value,key) in json 遍歷json對象中的值和鍵
(3)(value,key,index) in json 遍歷json對象中的值、鍵和索引
運行代碼:
<body> <div class="box"> <ul> <li v-for="value in json">{{value}}</li><br> <li v-for="(value,key) in json">{{value}}--{{key}}</li><br> <li v-for="(value,key,index) in json">{{value}}--{{key}}--{{index}}</li> </ul> </div> <script src="js/vue.js"></script> <script> new Vue({ el:".box", data:{ json:{ baidu:"百度", souhu:"搜狐", sougou:"搜狗" } } }); </script> </body>
輸出結果:
3.v-for遍歷整數
(1)n in 整數 遍歷1~整數,整數從1開始
(2)(n,index) in 整數 遍歷1~整數,整數從1開始,索引從0開始
運行代碼:
<body> <div class="box"> <ul> <li v-for="n in 3">{{n}}</li><br> <li v-for="(n,index) in 3">{{n}}--{{index}}</li> </ul> </div> <script src="js/vue.js"></script> <script> new Vue({ el:".box", data:{ } }); </script> </body>
輸出結果:
當然,v-for也可以在template中使用,但是這個我不想寫了