Home  >  Article  >  Web Front-end  >  How to use v-for in vue.js and how to get the index?

How to use v-for in vue.js and how to get the index?

青灯夜游
青灯夜游forward
2020-11-02 17:57:505837browse

The followingVue.js tutorial column will take you through the use of v-for and index acquisition in vue.js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

How to use v-for in vue.js and how to get the index?

2.x version:

v-for="(item,index) in items"

index is the index value.

==========================Separating line================ ==============

1.x version:

1 .v-for

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>
    <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. Index

Within the v-for block we have full access to the properties in the parent component scope. The special variable $index 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' }
    ]
  }
})

Additionally, you can specify an alias for the index (if v-for is used for an object, you can specify an alias for the object's key) :

<p v-for="(index, item) in items">
  {{ index }} {{ item.message }}</p>
Starting from 1.0.17, you can use of delimiter, which is closer to JavaScript traverser syntax:

<p v-for="item of items"></p>

Example 2:

<!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. 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>
        <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>

# Method 2: Pass in the index value directly

Example four (similar to method two):

<!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>

The effect is the same as method one.

But when there is a link:

Although it does not conflict with indexing , but if you want to perform further operations on the jumped link, you cannot prevent the jump event:

If you want to directly The following methods can be used to transfer the index:

Example 5:

<!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>

Supplement:

4. About the difference between v-for version 2.0 and 1.x

Example 5 of version 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>

The changes are as follows:

  1. el处需id,写body报错;
  2. 参数index需写在item后面;
  3. 作为事件参数时不用加$符。

  此外,也可以提供第二个的参数为键名:

<p v-for="(value, key) in object">

  {{ key }} : {{ value }}</p>

  第三个参数为索引:

<p v-for="(value, key, index) in object">

  {{ index }}. {{ key }} : {{ value }}</p>

 

 

相关推荐:

2020年前端vue面试题大汇总(附答案)

vue教程推荐:2020最新的5个vue.js视频教程精选

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of How to use v-for in vue.js and how to get the index?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:cnblogs.com. If there is any infringement, please contact admin@php.cn delete