vue.js中v-for的使用及索引獲取

2.x版本:javascript

 

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

index即索引值。css

 ==========================分割線==============================html

1.x版本:vue

1.v-forjava

  示例一:數組

<!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.索引this

  在 v-for 塊內咱們能徹底訪問父組件做用域內的屬性,特殊變量 $index是當前數組元素的索引:spa

<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 用於一個對象,則能夠爲對象的鍵指定一個別名):scala

<div v-for="(index, item) in items">
  {{ index }} {{ item.message }}
</div>

   從 1.0.17 開始能夠使用 of 分隔符,更接近 JavaScript 遍歷器語法:3d

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

  示例二:

<!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.在點擊事件中取到索引

  方法一:添加自定義屬性

  示例三:

 

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

  方法二:直接傳入索引值

  示例四(和二差很少):

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

  效果與方法一相同。

  不過有連接時:

  與取索引雖然不衝突,可是若是要對所跳的連接作進一步操做,則沒法阻止跳轉事件:

  若是想直接傳索引能夠用如下方法:

  示例五:

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

 

補充:

 

4.關於v-for版本2.0與1.x的區別

  2.0版本的示例五:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <style type="text/css">
            a{display: block;}
        </style>
    </head>
    <body>
        <div id="for5">
            <a v-for="(item,index) in items" v-on:click="onclick(index)" href="javascript:void(0)">{{ index }}{{ item.text }}</a>
        </div>
        <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>

  變化以下:

  1. el處需id,寫body報錯;
  2. 參數index需寫在item後面;
  3. 做爲事件參數時不用加$符。

  此外,也能夠提供第二個的參數爲鍵名:

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

  {{ key }} : {{ value }}

</div>

  第三個參數爲索引:

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

  {{ index }}. {{ key }} : {{ value }}

</div>
相關文章
相關標籤/搜索