1.v-for
Directly upload the code.
Example 1:
<!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> <div id="didi-navigator"> <ul> <li v-for="tab in tabs"> {{ tab.text }} </li> </ul> </div> <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. Index
Within the v-for block we can fully access the properties in the parent component scope, and there is also a special variable $index, as you guessed, It is the index of the current array element:
<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' } ] } })
Alternatively, you can specify an alias for the index (if v-for is used with an object, you can specify an alias for the object's key):
<div v-for="(index, item) in items"> {{ index }} {{ item.message }} </div>
Starting from 1.0.17, you can use the of delimiter, which is closer to JavaScript traverser syntax:
Example Two:
<!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> <div v-if="isNaN(click)==false"> <span>你点击的索引为: {{ click }}</span> </div> <div v-else> <p class="text-danger">试着点击上方LI条目</p> </div> <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. Get the index in the click event
Method 1: Add custom attributes
Example 3:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> a{display: block;} </style> </head> <body> <div> <a v-for="(index,item) in items" data-index="{{index}}" v-on:click="onclick" href="http://www.baidu.com">{{ item.text }}</a> </div> <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>
Method 2: Directly pass in the index Value
Example 4 (similar to 2):
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> a{display: block;} </style> </head> <body> <div> <a v-for="(index,item) in items" v-on:click="onclick($index)" href="#">{{ item.text }}</a> </div> <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>
The effect is the same as method 1.
If you want to directly transfer the index, you can use the following method:
Example 5:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> a{display: block;} </style> </head> <body> <div> <a v-for="(index,item) in items" v-on:click="onclick($index)" href="javascript:void(0)">{{ item.text }}</a> </div> <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>