search

Home  >  Q&A  >  body text

javascript - Question about v-for loop in vuejs

How do vue.js traverse objects using a for in-like method? I want to get a certain field name and value in the object.

習慣沉默習慣沉默2701 days ago861

reply all(6)I'll reply

  • 高洛峰

    高洛峰2017-07-05 10:45:22

    <p id="box">
        <ul>
            <li v-for="val in arr" track-by="$index">
                {{val}}
               
            </li>
        </ul>
    </p>
    
    var vm=new Vue({
        el:'#box',
        data:{
           arr:['apple','banana','orange']
        },
        methods:{
            
        }
    });
    对于arr里面的$index 和 $key  在1.0之内的版本可以使用,在2.0之后就摒弃了,引入框架版本的时候一定要注意,在2.0还可以用  v-for="(val,index) in arr"   {{index}}
    

    reply
    0
  • 仅有的幸福

    仅有的幸福2017-07-05 10:45:22

    Don’t quite understand what it means?

    Do you want to use v-for to display it on the page?

    Or is it?

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-07-05 10:45:22

    Object.keys obtains the array composed of the keys of this object, v-for traverses the array, and obtains the fields in the form of obj[k]

    reply
    0
  • 大家讲道理

    大家讲道理2017-07-05 10:45:22

    <p id="box">
        <ul>
            <li v-for="(val,key) in arr" track-by="$index">
                {{key}}:{{val}}  //key是键,val是值           
            </li>
        </ul>
    </p>
    
    var vm=new Vue({
        el:'#box',
        data:{
           arr:['apple','banana','orange']
        }
    });

    reply
    0
  • PHP中文网

    PHP中文网2017-07-05 10:45:22

    v-for = ‘(item, index) in items’
    items array
    item array item
    index array item corresponding subscript
    Code must be wrapped in quotes

    reply
    0
  • ringa_lee

    ringa_lee2017-07-05 10:45:22

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

    reply
    0
  • Cancelreply