Search code examples
javascripthtmlvue.jsvuejs2v-for

How to console.log an item inside a v-for loop in Vue


I can't figure out how to do a console.log to see what item is in the ul as its being passed.

 <div v-for="(item, index) in todos" :key="index">
     <ul v-if="item" :load="console.log(item)">
         <li v-for="(value, key) in item" :key="key">
            <label v-bind:for="key">{{ key }}</label>
            <div v-bind:id="key">{{ value }}</div>
         </li>
     </ul>
 </div>

 var vm = new Vue({
    el: '#components-demo',
    data: {
        todos: [
            newData
        ]
    }
 })

Solution

  • you should define a method like :

      <ul v-if="item" :load="log(item)">
    

    in your script :

    var vm = new Vue({
      el: '#components-demo',
      data: {
        todos: [
          newData
        ]
      },
      methods: {
        log(item) {
          console.log(item)
        }
      }
    })