ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

vue_列表渲染v-for

2020-03-05 12:02:37  阅读:156  来源: 互联网

标签:index vue 渲染 list length 列表 goodsId goodsPrice


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style type="text/css">
    .activated {color: red}
  </style>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
  <div id="app">
    <!-- 列表渲染 v-for  -->
    <!-- v-for="(item[, index]) of list"  item 表示当前项,index可选,表示当先项的下标-->
    <!-- key:唯一的,提升性能 -->
    <!-- 动态添加数据 不能使用下标插数据 
      vm.list[3] = {goodsId:"0004", "goodsPrice": "¥1299.00"}数据发生该变,但是不会渲染页面
      动态改变必须通过遍历方法改变,例如push pop slice 数组的方法,或者直接改变数组引用vm.list = [{},{},{}]
      -->
    <button @click="handleIndexLoad">下标动态加载数据</button>
    <button @click="handleForeachload">遍历动态加载数据</button>
    <div v-for="(item, index) of list" 
         :key="item.goodsId"
      >
      <span>{{item.goodsPrice}}</span>
      <span>---{{index}}</span>
    </div>
    <p>渲染占位符 只会渲染template包裹的元素</p>
    <template v-for="(item, index) of list">
      <span>{{item.goodsPrice}}</span>
      <span>---{{index}}</span>
    </template>
    <p>对象遍历</p>
    <!-- v-for='(value[, key, index]) of listObj' -->
    <!-- 
      value,对象每个属性的值
      key,对象每个属性
      index,下标索引
     -->
    <!-- 动态添加数据 不能像js一样直接添加属性obj.key = value 数据会发生改变但页面不会渲染 -->
    <template v-for="(value, key, index) of listObj" :key="value">
      <p>{{value}} --- {{key}} --- {{index}}</p>
    </template>
  </div>
</body>
<script type="text/javascript">
  let vm = new Vue({
    el: '#app',
    data: {
      list: [{
        "goodsId": "0001",
        "goodsPrice": "¥5899.00"
      },{
        "goodsId": "0002",
        "goodsPrice": "¥3599.00"
      },{
        "goodsId": "0003",
        "goodsPrice": "¥1299.00"
      }],
      listObj: {
        "goodsId": "0001",
        "goodsPrice": "¥5899.00",
        "goodsInfo": "直降500元!到手价5899元!享12期免息!低至16.2元/天",
        "goodsShop": "华为京东自营官方旗...",
        "goodsImg": "http://..............."
      }

    },
    methods: {
      handleIndexLoad () {
        this.list[this.list.length] = {
          "goodsId": '000'+(this.list.length + 1),
          "goodsPrice": "¥399.00"
        }
        console.log(this.list)
      },
      handleForeachload () {
        this.list.push({
          "goodsId": '000'+(this.list.length + 1),
          "goodsPrice": "¥399.00"
        })
        console.log(this.list)
      }
    }
  })
  
</script>
</html>

 

标签:index,vue,渲染,list,length,列表,goodsId,goodsPrice
来源: https://www.cnblogs.com/JunLan/p/12419378.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有