vue-5-列表渲染
一个数组的v-for
<ul id="example-1">
<li v-for="item in items">
{{ item.message }}
</li>
</ul>
var example1 = new Vue({
el: '#example-1',
data: {
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
//也可以用 of 替代 in 作为分隔符
<div v-for="item of items"></div>
//支持一个可选的第二个参数为当前项的索引
<ul id="example-2">
<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>
</ul>
var example2 = new Vue({
el: '#example-2',
data: {
parentMessage: 'Parent',
items: [
{ message: 'Foo' },
{ message: 'Bar' }
]
}
})
一个对象的v-for:
<div v-for="(value, key) in object">
{{ key }}: {{ value }}
</div> //第三个参数为索引
<div v-for="(value, key, index) in object">
key:
<div v-for="item in items" :key="item.id">
<!-- 内容 -->
</div>
//v-bind 来绑定唯一动态值,用来响应数据项的顺序改变等
数组更新检测:
//变异方法,会改变被这些方法调用的原始数组,会观察数组,触发视图更新
push()
pop()
shift()
unshift()
splice()
sort()
reverse()
//替换数组:返回一个新数组,如filter(), concat() 和 slice()
example1.items = example1.items.filter(function (item) {
return item.message.match(/Foo/)
})
数组检测注意事项:
?是不适当的做法,+是替代方案;
? 当你利用索引直接设置一个项时,例如:vm.items[indexOfItem] = newValue
+Vue.set(example1.items, indexOfItem, newValue)
+example1.items.splice(indexOfItem, , newValue)
//splice返回的是含有被删除的元素的新数组 ? 当你修改数组的长度时,例如:vm.items.length = newLength
+example1.items.splice(newLength)
对象检测注意事项:
//对于已经创建的实例,Vue 不能动态添加根级别的响应式属性
//使用 Vue.set(object, key, value) 方法向嵌套对象添加响应式属性 var vm = new Vue({
data: {
userProfile: {
name: 'Anika'
}
}
}) Vue.set(vm.userProfile, 'age', ) //vm.$set 实例方法,它只是全局 Vue.set 的别名
this.$set(this.userProfile, 'age', ) //赋多个属性,使用Object.assign或者_.extend()时,组合成一个新对象
this.userProfile = Object.assign({}, this.userProfile, {
age: ,
favoriteColor: 'Vue Green'
})
过滤、排序
//不实际改变或重置原始数据,可以创建返回过滤或排序数组的计算属性。
<li v-for="n in evenNumbers">{{ n }}</li>
data: {
numbers: [ , , , , ]
},
computed: {
evenNumbers: function () {//返回偶数
return this.numbers.filter(function (number) {
return number % ===
})
}
}
//或者,一个method();
<li v-for="n in even(numbers)">{{ n }}</li>
data: {
numbers: [ , , , , ]
},
methods: {
even: function (numbers) {
return numbers.filter(function (number) {
return number % ===
})
}
}
v-for 渲染多个元素
<ul>
<template v-for="item in items">
<li>{{ item.msg }}</li>
<li class="divider"></li>
</template>
</ul>
v-for与v-if在同一个元素上时,v-for的优先级更高
<li v-for="todo in todos" v-if="!todo.isComplete">
{{ todo }}
</li>
//每个循环中都会调用v-if
一个组件的v-for
<my-component
v-for="(item, index) in items"
v-bind:item="item"
v-bind:index="index"
v-bind:key="item.id"
></my-component>
//key是必需项
为了传递迭代数据到组件里,我们要用 props
Vue.component('todo-item', {
template: '\
<li>\
{{ title }}\
<button v-on:click="$emit(\'remove\')">X</button>\
</li>\
',
props: ['title']
})
<li
is="todo-item"
v-for="(todo, index) in todos"
v-bind:key="todo.id"
v-bind:title="todo.title"
v-on:remove="todos.splice(index, 1)"
></li>
vue-5-列表渲染的更多相关文章
- vue基础——列表渲染
列表渲染 用 v-for 把一个数组对应为一组元素 我们用 v-for 指令根据一组数组的选项列表进行渲染.v-for 指令需要使用 item in items 形式的特殊语法, items 是源数据 ...
- 【03】Vue 之列表渲染及条件渲染
3.1. 条件渲染 有时候我们要根据数据的情况,决定标签是否进行显示或者有其他动作.最常见的就是,表格渲染的时候,如果表格没有数据,就显示无数据.如果有数据就显示表格数据. Vue帮我们提供了一个v- ...
- vue基础---列表渲染
首先简单回顾下v-for‘指令 <ol id="list_area"> <li v-for="book in books">{{book ...
- 一起学Vue之列表渲染
在Vue开发中,列表数据绑定非常简单易用,本文主要通过一些简单的小例子,讲述v-for的使用方法,仅供学习分享使用,如有不足之处,还请指正. 用 v-for 把一个数组对应为一组元素 我们可以用 v- ...
- 前端框架之Vue(6)-列表渲染
用v-for把一个数组对应为一组元素 我们用 v-for 指令根据一组数组的选项列表进行渲染. v-for 指令需要使用 item in items 形式的特殊语法, items 是源数据数组并且 i ...
- vue之列表渲染
一.v-for循环用于数组 v-for 指令根据一组数组的选项列表进行渲染. 1.v-for 指令需要使用 item in items 形式的特殊语法,items 是源数据数组名, item 是数组元 ...
- vue笔记-列表渲染
用v-for把一个数组对应为一组元素 使用方法:v-for="(item,index) in items"//也可以使用of替代in { items:源数组 item:数组元素迭代 ...
- 关于vue.js中列表渲染练习
html: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8 ...
- 03-Vue入门系列之Vue列表渲染及条件渲染实战
3.1. 条件渲染 有时候我们要根据数据的情况,决定标签是否进行显示或者有其他动作.最常见的就是,表格渲染的时候,如果表格没有数据,就显示无数据.如果有数据就显示表格数据. Vue帮我们提供了一个v- ...
- 3-7 Vue中的列表渲染
举个案例:循环data中的list的值在div中,并显示相应的index值. 关于数组的循环: //显示效果如下图: //一般的列表渲染最好带一个key值,要把key值设置为唯一值的话,可以选择in ...
随机推荐
- Mac python Tesseract 验证码识别
Tesseract 简介 Tesseract(/'tesərækt/) 这个词的意思是"超立方体",指的是几何学里的四维标准方体,又称"正八胞体".不过这里要讲 ...
- shell和shell脚本基本知识
详情可见: https://www.cnblogs.com/yinheyi/p/6648242.html 这张图,可以帮助我们理解这个词语! 最底下是计算机硬件,然后硬件被系统核心包住,在系统核心外层 ...
- (转)C# System.Diagnostics.Process.Start使用
经常会遇到在Winform或是WPF中点击链接或按钮打开某个指定的网址, 或者是需要打开电脑中某个指定的硬盘分区及文件夹, 甚至是"控制面板"相关的东西, 如何做呢? 方法:使用 ...
- JavaScript 第一章总结
A quick dip into javascipt The way JavaScript works HTML 用一系列的 markup 来呈现整个 content 的 structure.CSS ...
- Http Requests for PHP
一.Requests for PHP 官网:http://requests.ryanmccue.info官方介绍:Requests is a humble HTTP request library. ...
- 围棋规则 - AlphaGO
参考:4分钟了解围棋规则 看懂柯洁和AlphaGo的对决并不难 围棋规则: 1. 19X19的棋盘上有361个落子点: 2. 黑白棋子依次落子: 3. 比赛结束时,占地多者胜: 4. 上下左右相邻的棋 ...
- English trip M1 - AC11 May I Help You? 我能帮到你吗? Teacher:Lamb
In this lesson you will learn to ask for things in shops 在本课程中,您将学习如何在商店中寻找东西 课上内容(Lesson) How are ...
- PCM数据格式
PCM数据格式 1. 音频简介 经常见到这样的描述: 44100HZ 16bit stereo 或者 22050HZ 8bit mono 等等. 44100HZ 16bit ster ...
- HDOJ-1156 Brownie Points II 线段树/树状数组(模板)
http://acm.hdu.edu.cn/showproblem.php?pid=1156 在一张二位坐标系中,给定n个点的坐标,玩一个划线游戏(线必须穿过点),Stan先手画一条垂直的线,然后Ol ...
- Create a Hadoop Build and Development Environment
Create a Hadoop Build and Development Environment http://vichargrave.com/create-a-hadoop-build-and-d ...