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 ...
随机推荐
- ThinkPHP表单自动验证(注册功能)
控制器中: 模型中: 视图中:
- android 趟坑记
又是一个伤感的故事,但阿古好像已经习以为常了. 大半年的辛苦又泡汤了,故事是这样. 帝都某高端小区,封闭局域网,做一个可视对讲+门禁的APP,之前那一版因为使用了商业代码,又不想花钱,于是找阿古换一个 ...
- [mybatis-spring] Transaction 事务/事务处理/事务管理器
使用mybatis-spring的主要原因之一就是: mybatis-spring允许mybatis参与到spring 事务中. mybatis-spring leverage[use (someth ...
- datatabe 与string
DataTable到string /// <summary> /// DataTable 到 string /// </summary> ...
- 关于Mysql 查询所有表的实时记录用于对比2个MySQL 库的数据是否异步
Xu言: 今天,为了研究一个MySQL主从同步开机后报错 问题,如下图 故障原因分析: 经过分析,可能是主从服务器开机顺序导致.(有待下次断电再次测试) 主从错误提示:日志读取错误的问题.解决方法:更 ...
- WPF自定义漂亮的按钮样式
首先打开 Microsoft Visual Studio 2008 ,新建一个WPF项目,在上面随便放几个按钮: 然后给各个按钮设置不同的背景颜色: 设置好之后就是这样啦: 然后我们就开始在 App. ...
- LeetCode--026--删除排序数组中的重复项
问题描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- Binary Gap(二进制空白)
中文标题[二进制空白] 英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zer ...
- Vue-router中的导航钩子
vue-router中的导航钩子,主要用来作用是拦截导航,让他完成跳转或取消.(路由守卫) 原文指路:https://blog.csdn.net/weixin_41399785/article/det ...
- python-flask-SQLAlchemy
SQLAlchemy 一. 介绍 SQLAlchemy是一个基于Python实现的ORM框架.该框架建立在 DB API之上,使用关系对象映射进行数据库操作,简言之便是:将类和对象转换成SQL,然后使 ...