Tuesday, January 15, 2019

Vue.js How to use v-for

The Result

  • The message is {{ item.message }} and the name is {{ item.name }} (age {{ item.age }}).

The code

<ul id="example-1">
   <li v-for="item in items">The message is {{ item.message }}, and the name is {{ item.name }} (age {{ item.age }}).</li>
</ul>

<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.js"></script>
<script type="application/javascript">
var example1 = new Vue({
  el: '#example-1',
  data: {
    items: [
      { message: 'Foo', name: 'John', age: '25' },
      { message: 'Bar', name: 'Mike', age: '30' },
      { message: 'FooBar', name: 'Jack', age: '18' }
    ]
  }
})
</script>

The official document