1.数组列表       v-for 块中,我们拥有对父作用域属性的完全访问权限。v-for 还支持一个可选的第二个参数为当前项的索引

1.1 普通渲染       v-for="item in items"     /    v-for="item of items"    /

v-for="item of items":key="item"      key  提升性能
v-for="(item,index) of items":key="index"     index 消除同名 key 风险  (排序问题)
<!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>
</head> <body> <ul id="example-1">
<li v-for="item in items">
{{ item.message }}
</li>
</ul> <script src="vue.js"></script> <script>
var example1 = new Vue({
el: '#example-1',
data: {
items: [{
message: 'Foo'
},
{
message: 'Bar'
}
]
}
})
</script> </body> </html>

1.1.1 简单 todolist 小实例

html

<!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="js/vue.js"></script>
</head>
<body>
<div id="app">
<input v-model="inputVal" />
<button @click="addList">add</button>
<ul>
<li v-for="(item,index) of items":key="index">{{item}}</li>
</ul>
</div>
<script src="js/js.js"></script> </body>
</html>

js

var app=new Vue({
el:'#app',
// template:'<h1>hello {{mes}}</h1>',
data:{
inputVal:'',
items:[]
},
methods:{
addList:function(){
this.items.push(this.inputVal);
this.inputVal='';
}
}
});

1.1.2 todo-list   组件化

html

<!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="js/vue.js"></script>
</head>
<body>
<div id="app">
<input v-model="inputVal" />
<button @click="addList">add</button>
<ul>
<todo-list
v-for="(item,index) of items":key="index"
:content="item"
></todo-list>
<!-- <li v-for="(item,index) of items":key="index">{{item}}</li> -->
</ul>
</div>
<script src="js/js.js"></script> </body>
</html>

js

// global component
// Vue.component('todo-list',
// {
// props:['content'],
// template:'<li>{{content}}</li>'}
// ); // local
var TodoItem={
props:['content'],
template:'<li>{{content}}</li>'
};
var app=new Vue({
el:'#app',
components:{
'todo-list':TodoItem
},
data:{
inputVal:'',
items:[]
},
methods:{
addList:function(){
this.items.push(this.inputVal);
this.inputVal='';
}
}
});

1.1.3 todo-list 父子组件之间传递参数、处理程序、发布 -  订阅模式

html

<!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="js/vue.js"></script>
</head>
<body>
<div id="app">
<input v-model="inputVal" />
<button @click="addList">add</button>
<ul>
<todo-list
v-for="(item,index) of items":key="index"
:content="item"
:index="index"
@del="removeHandle"
></todo-list>
<!-- <li v-for="(item,index) of items":key="index">{{item}}</li> -->
</ul>
</div>
<script src="js/js.js"></script> </body>
</html>

js

// global component
Vue.component('todo-list',{
props:['content','index'],
template:'<li @click="removeCall">{{content}}</li>',
methods:{
removeCall:function(){
this.$emit('del',this.index); // 发布事件 del ,传入参数 index
}
}
}); // 传递媒介:
// 父组件 - 子组件 属性
// 子组件 - 父组件 发布 - 订阅 、 父组件预定义方法接受 // local
// var TodoItem={
// props:['content'],
// template:'<li>{{content}}</li>'
// };
var app=new Vue({
el:'#app',
// components:{
// 'todo-list':TodoItem
// },
data:{
inputVal:'',
items:[]
},
methods:{
addList:function(){
this.items.push(this.inputVal);
this.inputVal='';
},
removeHandle:function(index){ // 父组件 - 接受处理程序
this.items.splice(index,1);
}
}
});

1.2 带索引渲染    v-for="(item, index) in items"

<!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>
</head> <body> <ul id="example-2">
<li v-for="(item, index) in items">
{{ parentMessage }} - {{ index }} - {{ item.message }}
</li>
</ul> <script src="vue.js"></script> <script>
var example2 = new Vue({
el: '#example-2',
data: {
parentMessage: 'Parent',
items: [{
message: 'Foo'
},
{
message: 'Bar'
}
]
}
})
</script> </body> </html>

2. 对象属性列表

2.1 普通渲染     ( 普通的js对象不加引号 ,   json 文件 默认为属性打上引号,同构造函数 大写一样,是一个默认)

<!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>
</head> <body> <ul id="v-for-object" class="demo">
<li v-for="value in object">
{{ value }}
</li>
</ul> <script src="vue.js"></script> <script>
new Vue({
el: '#v-for-object',
data: {
object: {
title: 'How to do lists in Vue',
author: 'Jane Doe',
publishedAt: '2016-04-10'
}
}
})
</script> </body> </html>

2.2 带属性值

<div v-for="(value, name) in object"> {{ name }}: {{ value }} </div>

2.3 带属性值和索引

<div v-for="(value, name, index) in object"> {{ index }}. {{ name }}: {{ value }} </div>

最新文章

  1. Eclipse部署Maven web项目到tomcat服务器时,没有将lib下的jar复制过去的解决办法
  2. python 函数式编程工具
  3. C primer plus 练习题 第一章
  4. noip2016酱油记day1
  5. C++学习之const整理总结
  6. [Ruby on Rails系列]2、开发环境准备:Ruby on Rails开发环境配置
  7. CentOS6.5安装MySQL及完全卸载
  8. 类和对象:面向对象编程 - 零基础入门学习Python037
  9. 找到你在网页中缓存起来的flash文件
  10. 微信小程序添加、删除class’
  11. Poj2723:Get Luffy Out
  12. 在ASP.NET Core中使用EPPlus导入出Excel文件
  13. [LeetCode] Cheapest Flights Within K Stops K次转机内的最便宜的航班
  14. 【BUAA-OO】第一单元作业总结
  15. Bootstrap之信息记录
  16. webDriver文档阅读笔记
  17. 微信小程序的布局css样式
  18. 算法笔记--字典树(trie 树)&amp;&amp; ac自动机 &amp;&amp; 可持久化trie
  19. SQL--数据--基本操作
  20. sqlserver数据库的分离与附加

热门文章

  1. MQTT协议 Websocket JS客户端
  2. spring的IOC——依赖注入的两种实现类型
  3. 后盾网lavarel视频项目---模型一对多关联简单实例
  4. 后盾网lavarel视频项目---图片上传
  5. 阿里镜像源配置yum
  6. 使用 sed 命令查找和替换文件中的字符串的 16 个示例
  7. CRC32算法C#中的实现
  8. loggin模块,错误日志模块
  9. python3使用ltp语言云
  10. Selenium+Python的开发环境搭建