溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

vue.js中怎么使用v-for

發(fā)布時(shí)間:2020-12-10 10:05:57 來源:億速云 閱讀:170 作者:小新 欄目:編程語言

小編給大家分享一下vue.js中怎么使用v-for,希望大家閱讀完這篇文章后大所收獲,下面讓我們一起去探討吧!

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: '順風(fēng)車' },
                    { text: '出租車' },
                    { text: '代駕' }
                ]
            }
        })    </script></body></html>

vue.js中怎么使用v-for

2.索引

  在 v-for 塊內(nèi)我們能完全訪問父組件作用域內(nèi)的屬性,特殊變量 $index是當(dāng)前數(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' }
    ]
  }
})

vue.js中怎么使用v-for

  另外,你可以為索引指定一個(gè)別名(如果 v-for 用于一個(gè)對(duì)象,則可以為對(duì)象的鍵指定一個(gè)別名):

<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>你點(diǎn)擊的索引為: {{ click }}</span>
    </p>
    <p v-else>
        <p class="text-danger">試著點(diǎn)擊上方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>

vue.js中怎么使用v-for

3.在點(diǎn)擊事件中取到索引

  方法一:添加自定義屬性

  示例三:

<!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: '順風(fēng)車' },
                    { 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>

vue.js中怎么使用v-for

  方法二:直接傳入索引值

  示例四(和二差不多):

<!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: '順風(fēng)車' },

     { text: '出租車' },

     { text: '代駕' }

     ]

     },

    methods: {

     onclick:function(index){//      index.preventDefault();
    console.log(index);

    document.getElementById('index').value = index;

}

    }

})</script></body></html>

  效果與方法一相同。

  不過有鏈接時(shí):

vue.js中怎么使用v-for

  與取索引雖然不沖突,但是如果要對(duì)所跳的鏈接做進(jìn)一步操作,則無法阻止跳轉(zhuǎn)事件:

vue.js中怎么使用v-for

  如果想直接傳索引可以用以下方法:

  示例五:

<!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: '順風(fēng)車' },
                    { 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>

補(bǔ)充:

4.關(guān)于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: '順風(fēng)車' },
                    { 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報(bào)錯(cuò);
  2. 參數(shù)index需寫在item后面;
  3. 作為事件參數(shù)時(shí)不用加$符。

  此外,也可以提供第二個(gè)的參數(shù)為鍵名:

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

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

  第三個(gè)參數(shù)為索引:

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

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

看完了這篇文章,相信你對(duì)vue.js中怎么使用v-for有了一定的了解,想了解更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI