小編給大家分享一下vue.js中怎么使用v-for,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!
創(chuàng)新互聯(lián)堅持“要么做到,要么別承諾”的工作理念,服務領域包括:成都網站設計、成都網站制作、企業(yè)官網、英文網站、手機端網站、網站推廣等服務,滿足客戶于互聯(lián)網時代的張北網站設計、移動媒體設計的需求,幫助企業(yè)找到有效的互聯(lián)網解決方案。努力成為您成熟可靠的網絡建設合作伙伴!2.x版本:
v-for="(item,index) in items"
index即索引值。
==========================分割線==============================
1.x版本:
1.v-for
示例一:
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title></title></head><body> <p id="didi-navigator"> <ul> <li v-for="tab in tabs"> {{ tab.text }} </li> </ul> </p> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#didi-navigator', data: { tabs: [ { text: '巴士' }, { text: '快車' }, { text: '專車' }, { text: '順風車' }, { text: '出租車' }, { text: '代駕' } ] } }) </script></body></html>
2.索引
在 v-for 塊內我們能完全訪問父組件作用域內的屬性,特殊變量 $index是當前數(shù)組元素的索引:
<ul id="example-2"> <li v-for="item in items"> {{ parentMessage }} - {{ $index }} - {{ item.message }} </li></ul>
var example2 = new Vue({ el: '#example-2', data: { parentMessage: 'Parent', items: [ { message: 'Foo' }, { message: 'Bar' } ] } })
另外,你可以為索引指定一個別名(如果 v-for 用于一個對象,則可以為對象的鍵指定一個別名):
<p v-for="(index, item) in items"> {{ index }} {{ item.message }}</p>
從 1.0.17 開始可以使用 of 分隔符,更接近 JavaScript 遍歷器語法:
<p v-for="item of items"></p>
示例二:
<!DOCTYPE html><html><head> <meta charset="utf-8"> <meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" /> <title></title></head><body> <ul> <li v-for="option in options"> <p class="text-success" v-on:click="getIndex($index)">Text:{{option.text}}--Vlue:{{option.value}}</p> </li> </ul> <p v-if="isNaN(click)==false"> <span>你點擊的索引為: {{ click }}</span> </p> <p v-else> <p class="text-danger">試著點擊上方LI條目</p> </p> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: 'body', data: { click: 'a', options: [ { text: '上海市', value: '20' }, { text: '湖北省', value: '43' }, { text: '河南省', value: '45' }, { text: '北京市', value: '10' } ] }, methods:{ getIndex:function($index){ this.click=$index; } } }); </script></body></html>
3.在點擊事件中取到索引
方法一:添加自定義屬性
示例三:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> a{display: block;} </style> </head> <body> <p> <a v-for="(index,item) in items" data-index="{{index}}" v-on:click="onclick" href="http://www.baidu.com">{{ item.text }}</a> </p> <input type="text" name="" id="index" value=""/> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: 'body', data: { items: [ { text: '巴士' }, { text: '快車' }, { text: '專車' }, { text: '順風車' }, { text: '出租車' }, { text: '代駕' } ] }, methods: { onclick:function(event){ event.preventDefault(); let target = event.target console.log(target.getAttribute("data-index")); document.getElementById('index').value = target.getAttribute("data-index"); } } }) </script> </body></html>
方法二:直接傳入索引值
示例四(和二差不多):
<!DOCTYPE html><html><head><meta charset="UTF-8"><title></title><style type="text/css">a{display: block;}</style></head><body><p> <a v-for="(index,item) in items" v-on:click="onclick($index)" href="#">{{ item.text }}</a></p><input type="text" name="" id="index" value=""/><script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: 'body', data: { items: [ { text: '巴士' }, { text: '快車' }, { text: '專車' }, { text: '順風車' }, { text: '出租車' }, { text: '代駕' } ] }, methods: { onclick:function(index){// index.preventDefault(); console.log(index); document.getElementById('index').value = index; } } })</script></body></html>
效果與方法一相同。
不過有鏈接時:
與取索引雖然不沖突,但是如果要對所跳的鏈接做進一步操作,則無法阻止跳轉事件:
如果想直接傳索引可以用以下方法:
示例五:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> a{display: block;} </style> </head> <body> <p> <a v-for="(index,item) in items" v-on:click="onclick($index)" href="javascript:void(0)">{{ item.text }}</a> </p> <input type="text" name="" id="index" value=""/> <script src="js/vue.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: 'body', data: { items: [ { text: '巴士' }, { text: '快車' }, { text: '專車' }, { text: '順風車' }, { text: '出租車' }, { text: '代駕' } ] }, methods: { onclick:function(index){// index.preventDefault(); console.log(index); document.getElementById('index').value = index; window.location.href = "http://www.baidu.com"; } } }) </script> </body></html>
補充:
4.關于v-for版本2.0與1.x的區(qū)別
2.0版本的示例五:
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> a{display: block;} </style> </head> <body> <p id="for5"> <a v-for="(item,index) in items" v-on:click="onclick(index)" href="javascript:void(0)">{{ index }}{{ item.text }}</a> </p> <input type="text" name="" id="index" value=""/> <script src="js/vue2.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> new Vue({ el: '#for5', data: { items: [ { text: '巴士' }, { text: '快車' }, { text: '專車' }, { text: '順風車' }, { text: '出租車' }, { text: '代駕' } ] }, methods: { onclick:function(index){ console.log(index); document.getElementById('index').value = index;// window.location.href = "http://www.baidu.com"; window.location.href = "#"; } } }) </script> </body></html>
變化如下:
此外,也可以提供第二個的參數(shù)為鍵名:
<p v-for="(value, key) in object"> {{ key }} : {{ value }}</p>
第三個參數(shù)為索引:
<p v-for="(value, key, index) in object"> {{ index }}. {{ key }} : {{ value }}</p>
看完了這篇文章,相信你對vue.js中怎么使用v-for有了一定的了解,想了解更多相關知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
當前標題:vue.js中怎么使用v-for-創(chuàng)新互聯(lián)
本文地址:http://bm7419.com/article32/ddhssc.html
成都網站建設公司_創(chuàng)新互聯(lián),為您提供品牌網站制作、微信小程序、Google、軟件開發(fā)、用戶體驗、網站設計
聲明:本網站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)