美文网首页
vue官方文档 v-for学习笔记

vue官方文档 v-for学习笔记

作者: 徐慵仙 | 来源:发表于2020-04-12 10:20 被阅读0次

官方文档地址

列表渲染 https://cn.vuejs.org/v2/guide/list.html

简述

通过练习文档中例子,掌握以下知识点:

  • v-for 的基础用法
  • v-for 遍历对象的方法
  • v-for 遍历对象 (A,B,C)分别代表 A-属性值 B-属性名 C-索引值
  • 数组更新的七个方法,这些方法可以原地更新数组的值
    • push()
    • pop()
    • shift()
    • unshift()
    • splice()
    • sort()
    • reverse()
  • 替换数组的方法,这些方法不能原地更新数组值
  • 对数组应用计算属性

练习代码

<template>
  <div class="about">
    <h1>v-for列表渲染</h1>
    <hr />
    <h3>基础用法</h3>
    <ul id="example-1">
      <li v-for="item in items" :key="item.message">{{item.message}}</li>
    </ul>
    <h3>带index索引</h3>
    <ul id="example-1">
      <li v-for="(item,index) in items" :key="item.message">{{index}} - {{item.message}}</li>
    </ul>
    <hr />
    <h3>遍历对象</h3>
    <ul id="v-for-object" class="demo">
      <li v-for="value in object" :key="value">{{value}}</li>
    </ul>
    <h3>带属性名</h3>
    <ul id="v-for-object" class="demo">
      <li v-for="(value,title) in object" :key="value">{{title}} : {{value}}</li>
    </ul>
    <h3>带index</h3>
    <ul id="v-for-object" class="demo">
      <li v-for="(value,title,index) in object" :key="value">{{index}} {{title}} : {{value}}</li>
    </ul>
    <hr />
    <h3>数组更新</h3>
    <button @click="arrPush">push</button>
    <button @click="arrPop">pop</button>
    <button @click="arrShift">shift</button>
    <button @click="arrUnshift">unshift</button>
    <button @click="arrSplice">splice</button>
    <button @click="arrSort">sort</button>
    <button @click="arrReverse">reverse</button>
    <button @click="arrFilter">保留偶数</button>
    <ul>
      <li v-for="(num,index) in numbers" :key="index">{{num}}</li>
    </ul>
    <hr />
    <h3>计算属性</h3>
    <ul>
      <li v-for="(num,index) in evenNumbers" :key="index">{{num}}</li>
    </ul>
    <hr />
    <h3>for循环中使用方法</h3>
    <ul v-for="(set,index) in sets" :key="index">
      <li v-for="n in even(set)" :key="n">{{ n }}</li>
    </ul>
    <hr />
    <h3>v-for使用值范围</h3>
    <div>
      <span v-for="n in 10" :key="n">{{n}}</span>
    </div>
    <hr />
    <h3>template使用v-for渲染一段元素:测试证明这样写不行,key只能写在ul上</h3>
    <ul v-for="item in items" :key="item.message">
      <template>
        <li>{{ item.message }}</li>
        <li class="divider" role="presentation"></li>
      </template>
    </ul>
  </div>
</template>
<script>
export default {
  data() {
    return {
      items: [{ message: "Foo" }, { message: "Bar" }],
      object: {
        title: "How to do lists in Vue",
        author: "Jane Doe",
        publishedAt: "2016-04-10"
      },
      numbers: [1, 5, 2, 3],
      sets: [
        [1, 2, 3, 4, 5],
        [6, 7, 8, 9, 10]
      ]
    };
  },
  methods: {
    arrPush() {
      this.numbers.push(4);
    },
    arrPop() {
      this.numbers.pop();
    },
    arrShift() {
      this.numbers.shift();
    },
    arrUnshift() {
      this.numbers.unshift(9);
    },
    arrSplice() {
      //替换可用这个方法
      this.numbers.splice(0, 2, 10, 20);
    },
    arrSort() {
      this.numbers.sort();
    },
    arrReverse() {
      this.numbers.reverse();
    },
    arrFilter() {
      this.numbers = this.numbers.filter(function(item) {
        return item % 2 === 0;
      });
    },
    even(nums) {
      return nums.filter(function(num) {
        return num % 2 === 0;
      });
    }
  },
  computed: {
    evenNumbers: function() {
      return this.numbers.filter(function(item) {
        return item % 2 === 0;
      });
    }
  }
};
</script>

相关文章

  • Vue 2.5从零开始学习 — v-if,v-show,v-f

    Vue 的 v-if,v-show,v-for 指令 文档警告 Warning:由于做笔记没有注意到 Vue 官方...

  • vue官方文档 v-for学习笔记

    官方文档地址 列表渲染 https://cn.vuejs.org/v2/guide/list.html 简述 通...

  • cordova+vue开发Android&IOS

    本文章仅作为个人笔记 cordova官网 vue官网 cordova官方文档 vue官方文档 环境搭建相关就不介绍...

  • vue2.0—请求

    vue2.0—请求 构建项目和创建组件就不细说了,不懂的可以去官方文档API学习 VUE官方文档:https://...

  • vue2.0—父子通信

    vue2.0—父子通信 构建项目和创建组件就不细说了,不懂的可以去官方文档API学习 VUE官方文档:https:...

  • v-for 中的 key

    vue官方文档不推荐使用 index 作为 v-for 的 key,一直不明白为什么,开发中也没有碰到相关问题,今...

  • vue2.0—路由嵌套和路由传参

    vue2.0—路由传参 构建项目和创建组件就不细说了,不懂的可以去官方文档API学习 VUE官方文档:https:...

  • 入门

    Vue官方文档生命周期详解 vue-routervue-router官方文档vue-router详解

  • Vue常用文档记录

    最近正在学习Vue,对Vue常用的一些api文档地址进行总结(仅为方便自己查看与学习记录) 1、Vue官方文档 ...

  • 为什么要给列表类组件设个key?

    对于这个问题,官方文档上已经给出了详细的解释 当 Vue.js 用 v-for 正在更新已渲染过的元素列表时,它默...

网友评论

      本文标题:vue官方文档 v-for学习笔记

      本文链接:https://www.haomeiwen.com/subject/llhvmhtx.html