9.列表渲染

列表渲染

image-20220816142824547

我们可以使用 v-for 指令基于一个数组来渲染一个列表。v-for 指令的值需要使用 item in items 形式的特殊语法,其中 items 是源数据的数组,而 item 是迭代项的别名

<template>
  <div>
    <p v-for="item in names">{{ item }}</p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      names:["百战程序员","尚学堂","IT"]
     }
   }
}
</script>

复杂数据

大多数情况,我们渲染的数据源来源于网络请求,也就是 JSON 格式

<template>
  <div v-for="item in result">
    <p>{{ item.title }}</p>
    <img :src="item.avator" alt="">
  </div>
</template>
<script>
export default {
  data() {
    return {
      result: [{
        "id": 2261677,
        "title": "鄂尔多斯|感受一座城市的璀璨夜景 感受一座城市,除了白日里的车水马龙,喧嚣繁华之",
        "avator": "https://pic.qyer.com/avatar/002/25/77/30/200?v=1560226451",
       },
       {
        "id": 2261566,
        "title": "成都这家洞穴暗黑风咖啡厅酷毙了‼️早C晚A走起☕️ 成都天气这么热? 咖啡?人必",
        "avator": "https://pic.qyer.com/avatar/011/07/08/69/200?v=1572185180",
       },
       {
        "id": 2261662,
        "title": "【川西新龙—措卡湖】措卡湖,意为“乱石丛中的黑色海水”,神秘小众 原汁原味。 深",
        "avator": "https://pic.qyer.com/avatar/009/88/48/58/200?v=1507386782",
       },
       ]
     }
   }
}
</script>
<style>
img{
  width: 50px;
  height: 50px;
}
</style>

v-for 也支持使用可选的第二个参数表示当前项的位置索引

<template>
  <div>
    <p v-for="(item,index) in names">{{ index }}:{{ item }}</p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      names:["百战程序员","尚学堂","IT"]
     }
   }
}
</script>

你也可以使用 of 作为分隔符来替代 in,这更接近 JavaScript 的迭代器语法

<div v-for="item of items"></div>

v-for 与对象

你也可以使用 v-for 来遍历一个对象的所有属性

<template>
  <div>
    <p v-for="(value, key, index) of userInfo">{{ value }}-{{ key }}-{{ index }}</p>
  </div>
</template>
<script>
export default {
  data() {
    return {
      userInfo:{
        name:"iwen",
        age:20
       }
     }
   }
}
</script>

实时效果反馈

1. 在Vue中,使用v-for遍历数组,下列形式正确的是:

A item in items

B item for items

C item each items

D item map items

答案

1=>A

相关推荐

  1. 【Vue】列表渲染

    2024-04-21 20:48:03       53 阅读
  2. vue-列表渲染

    2024-04-21 20:48:03       45 阅读
  3. vue ---列表渲染

    2024-04-21 20:48:03       40 阅读
  4. Vue2学习笔记(列表渲染

    2024-04-21 20:48:03       51 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-21 20:48:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-21 20:48:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-04-21 20:48:03       87 阅读
  4. Python语言-面向对象

    2024-04-21 20:48:03       96 阅读

热门阅读

  1. TCP协议学习记录

    2024-04-21 20:48:03       33 阅读
  2. python零基础入门 (9)-- 模块与包

    2024-04-21 20:48:03       35 阅读
  3. git 的基本命令行

    2024-04-21 20:48:03       39 阅读
  4. flutter知识点---三棵树

    2024-04-21 20:48:03       39 阅读
  5. Vue3: 获取元素DOM的方法

    2024-04-21 20:48:03       50 阅读
  6. excel文件预览: luckyexcel+luckysheet

    2024-04-21 20:48:03       135 阅读
  7. 大数据:【学习笔记系列】flink和spark的区别

    2024-04-21 20:48:03       38 阅读
  8. MASA Framework系列-核心概念(2)

    2024-04-21 20:48:03       30 阅读
  9. WPF中TextBox失去焦点事件

    2024-04-21 20:48:03       41 阅读