网络日志

【Vue】v-for

完整可运行代码

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title></title>
  <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <h1> 遍历数组 </h1>
    <ul>
      <!-- for 优先级 高于 if -->
      <!-- 放一起报错 -->
      <li v-for="(item, index) in array" :key="item.id">
        {{ index }} - {{ item.value }}
      </li>
    </ul>
    <!--  -->
    <h1> 遍历对象 </h1>
    <ul>
      <li v-for="(value, key, index) in obj" :key="key">
        {{ index }} - {{ key }} - {{ value }}
      </li>
    </ul>
  </div>
</body>
<script>
  var app = new Vue({
    el: '#app',
    data: {
      array: [
        {id:1, value:'a'},
        {id:2, value:'b'},
        {id:3, value:'c'},
      ],
      obj: {
        name: 'tao1',
        hobby: 'reading',
      }
    }
  })
</script>
</html>