Vue中的v-for for-in for-of的用法

72 阅读1分钟
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="https://unpkg.com/vue@next"></script>
  </head>
  <body>
    <div id="root"></div>
  </body>
  <script>
    const app = Vue.createApp({
      data() {
        return {
            studentsObj: {
            name: "zhangsan",
            sex: "male",
            age: 18,
          },
        };
      },
      // v-for=((value,index)of Arr) 用来遍历数组。for of 里面是两个参数
      // v-for=((value name index)in obj)for in 里面是三个参数
    //   > 遍历数组时推荐使用 of,语法格式为`(item, index)`

// > - item:迭代时不同的数组元素的值
// > - index:当前元素的索引

// > 循环遍历对象时推荐使用 in,语法格式为`(item,name,index)`

// > - item:迭代时对象的键名键值
// > - name:迭代时对象的键名
// > - index:当前元素的索引

// > v-for 为了节约性能, 需要搭配 key 来使用, key 值要唯一
      template: `
            <ul>
                <li  v-for="(value,name,index) in studentsObj">
                    {value}--{{name}---{{index}}}
                    </li>
                </ul>
        `,
    });

    const vm = app.mount("#root");

  </script>
</html>