zoukankan      html  css  js  c++  java
  • 黑马vue---18、v-for指令的四种使用方式

    黑马vue---18、v-for指令的四种使用方式

    一、总结

    一句话总结:

    (item, i) in list:什么in什么的形式,前面是各种参数

    1、v-for循环普通数组?

    <p v-for="(item, i) in list">索引值:{{i}} --- 每一项:{{item}}</p>

    list: [1, 2, 3, 4, 5, 6]

    2、v-for循环对象数组?

    <p v-for="(user, i) in list">Id:{{ user.id }} --- 名字:{{ user.name }} --- 索引:{{i}}</p>

      list: [
        { id: 1, name: 'zs1' },
        { id: 2, name: 'zs2' },
        { id: 3, name: 'zs3' },
        { id: 4, name: 'zs4' }
      ]

    3、v-for循环对象?

    <p v-for="(val, key, i) in user">值是: {{ val }} --- 键是: {{key}} -- 索引: {{i}}</p>
    注意:在遍历对象身上的键值对的时候, 除了 有  val  key  ,在第三个位置还有 一个 索引

      user: {
        id: 1,
        name: '托尼·屎大颗',
        gender: '男'
      }

    4、v-for迭代数字?

    <p v-for="count in 10">这是第 {{ count }} 次循环</p>
    注意:如果使用 v-for 迭代数字的话,前面的 count 值从 1 开始

    二、内容在总结中

    1、Vue指令之v-forkey属性

    1. 迭代数组
    <ul>
      <li v-for="(item, i) in list">索引:{{i}} --- 姓名:{{item.name}} --- 年龄:{{item.age}}</li>
    </ul>
    
    1. 迭代对象中的属性
    
    	<!-- 循环遍历对象身上的属性 -->
    
        <div v-for="(val, key, i) in userInfo">{{val}} --- {{key}} --- {{i}}</div>
    
    
    1. 迭代数字
    
    <p v-for="i in 10">这是第 {{i}} 个P标签</p>
    
    

    2.2.0+ 的版本里,当在组件中使用 v-for 时,key 现在是必须的。

    当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默认用 “就地复用” 策略。如果数据项的顺序被改变,Vue将不是移动 DOM 元素来匹配数据项的顺序, 而是简单复用此处每个元素,并且确保它在特定索引下显示已被渲染过的每个元素。

    为了给 Vue 一个提示,以便它能跟踪每个节点的身份,从而重用和重新排序现有元素,你需要为每项提供一个唯一 key 属性。

     

    2、代码

    v-for循环普通数组:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <script src="./lib/vue-2.4.0.js"></script>
    </head>
    
    <body>
      <div id="app">
        <!-- <p>{{list[0]}}</p>
        <p>{{list[1]}}</p>
        <p>{{list[2]}}</p>
        <p>{{list[3]}}</p>
        <p>{{list[4]}}</p> -->
    
        <p v-for="(item, i) in list">索引值:{{i}} --- 每一项:{{item}}</p>
    
      </div>
    
      <script>
        // 创建 Vue 实例,得到 ViewModel
        var vm = new Vue({
          el: '#app',
          data: {
            list: [1, 2, 3, 4, 5, 6]
          },
          methods: {}
        });
      </script>
    </body>
    
    </html>

    v-for循环对象数组:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <script src="./lib/vue-2.4.0.js"></script>
    </head>
    
    <body>
      <div id="app">
        <p v-for="(user, i) in list">Id:{{ user.id }} --- 名字:{{ user.name }} --- 索引:{{i}}</p>
      </div>
    
      <script>
        // 创建 Vue 实例,得到 ViewModel
        var vm = new Vue({
          el: '#app',
          data: {
            list: [
              { id: 1, name: 'zs1' },
              { id: 2, name: 'zs2' },
              { id: 3, name: 'zs3' },
              { id: 4, name: 'zs4' }
            ]
          },
          methods: {}
        });
      </script>
    </body>
    
    </html>

    v-for循环对象:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <script src="./lib/vue-2.4.0.js"></script>
    </head>
    
    <body>
      <div id="app">
        <!-- 注意:在遍历对象身上的键值对的时候, 除了 有  val  key  ,在第三个位置还有 一个 索引  -->
        <p v-for="(val, key, i) in user">值是: {{ val }} --- 键是: {{key}} -- 索引: {{i}}</p>
      </div>
    
      <script>
        // 创建 Vue 实例,得到 ViewModel
        var vm = new Vue({
          el: '#app',
          data: {
            user: {
              id: 1,
              name: '托尼·屎大颗',
              gender: ''
            }
          },
          methods: {}
        });
      </script>
    </body>
    
    </html>

    v-for迭代数字:

    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
      <script src="./lib/vue-2.4.0.js"></script>
    </head>
    
    <body>
      <div id="app">
        <!-- in 后面我们放过  普通数组,对象数组,对象, 还可以放数字 -->
        <!-- 注意:如果使用 v-for 迭代数字的话,前面的 count 值从 1 开始 -->
        <p v-for="count in 10">这是第 {{ count }} 次循环</p>
      </div>
    
      <script>
        // 创建 Vue 实例,得到 ViewModel
        var vm = new Vue({
          el: '#app',
          data: {},
          methods: {}
        });
      </script>
    </body>
    
    </html>
     
  • 相关阅读:
    jmeter上传和下载、webservice、数据库连接 -- 9
    jmeter cookies和token -- 8
    java 获得 微信 UserId
    让textarea根据文本的长度自动调整它的高度
    oracle 连接数据库并查询,返回List<Map<String, Object>> 数据
    POI 4.0 读取Excel
    excel (2)
    导出 doc
    sui Mobile 试玩
    oracle 与 前台 md5
  • 原文地址:https://www.cnblogs.com/Renyi-Fan/p/11658107.html
Copyright © 2011-2022 走看看