vue2.0 之列表渲染-v-for
v-for 数组渲染
App.vue代码
<template>
<div>
<ul>
<li v-for="item in list"> {{ item.name }} - {{ item.price }}</li>
</ul>
<ul>
<li v-for="item in list" v-text="item.name + '-' + item.price"></li>
</ul>
<ul>
<li v-for="(item, index) in list"> {{ index }} - {{ item.name }} - {{ item.price }}</li>
</ul>
</div>
</template> <script>
export default {
data () {
return {
list: [
{
name: 'apple 7S',
price: 6188
},
{
name: 'huawei P10',
price: 4288
},
{
name: 'mi6',
price: 2999
}
]
}
}
}
</script> <style>
html {
height: 100%;
}
</style>
页面效果

v-for 对象渲染
App.vue代码
<template>
<div>
<ul>
<li v-for="value in objlist"> {{ value }}</li>
</ul>
<ul>
<li v-for="(key, value) in objlist"> {{ key + ':' + value }}</li>
</ul>
</div>
</template> <script>
export default {
data () {
return {
objlist: {
name: 'apple 7S',
price: 6188,
color: 'red',
size: 6.0
}
}
}
}
</script> <style>
html {
height: 100%;
}
</style>
页面显示

v-for 子组件渲染
创建./components/hello.vue文件
<template>
<div>
{{ hello }}
</div>
</template> <script>
export default {
data () {
return {
hello: 'i am component hello'
}
}
}
</script> <style scoped>/**/
h1 {
height: 100px;
}
</style>
App.vue代码
<template>
<div>
<componenthello v-for="(key, value) in objlist"></componenthello>
</div>
</template> <script>
import componenthello from './components/hello'
export default {
components: {
componenthello
},
data () {
return {
objlist: {
name: 'apple 7S',
price: 6188,
color: 'red',
size: 6.0
}
}
}
}
</script> <style>
html {
height: 100%;
}
</style>
页面显示

列表数据的同步更新
案例一:
<template>
<div>
<ul>
<li v-for="item in list"> {{ item.name }} - {{ item.price }}</li>
</ul>
<button v-on:click="addItem">addItem</button>
</div>
</template> <script>
export default {
data () {
return {
list: [
{
name: 'apple 7S',
price: 6188
},
{
name: 'huawei P10',
price: 4288
},
{
name: 'mi6',
price: 2999
}
]
}
},
methods: {
addItem () {
this.list.push({
name: 'meizu',
price: 2499
})
}
}
}
</script> <style>
html {
height: 100%;
}
</style>



案例二
<template>
<div>
<ul>
<li v-for="item in list"> {{ item.name }} - {{ item.price }}</li>
</ul>
<button v-on:click="addItem">addItem</button>
</div>
</template> <script>
import Vue from 'vue'
export default {
data () {
return {
list: [
{
name: 'apple 7S',
price: 6188
},
{
name: 'huawei P10',
price: 4288
},
{
name: 'mi6',
price: 2999
}
]
}
},
methods: {
addItem () {
Vue.set(this.list, 1, {
name: 'meizu',
price: 2499
})
}
}
}
</script> <style>
html {
height: 100%;
}
</style>
点击按钮前

点击按钮后

vue2.0 之列表渲染-v-for的更多相关文章
- vue2.0 之文本渲染-v-html、v-text
vue2.0 之文本渲染-v-html.v-text 1.index.html代码 <!DOCTYPE html> <html> <head> <meta c ...
- Vue2.0流式渲染中文乱码问题
在参照vue2.0中文官方文档学习服务端渲染之流式渲染时,因为响应头默认编码类型为GBK,而文件为UFT-8类型,所以出现了中文乱码问题. 解决办法:设置响应头编码类型即可 response.setH ...
- vue2.0 之条件渲染
条件渲染v-if.v-show <template> <div> <a v-if="isPartA">partA</a> <a ...
- Vue2.0 中,“渐进式框架”和“自底向上增量开发的设计”这两个概念是什么?(转)
https://www.zhihu.com/question/51907207?rf=55052497 徐飞 在我看来,渐进式代表的含义是:主张最少. 每个框架都不可避免会有自己的一些特点,从而会对使 ...
- 前端MVC Vue2学习总结(四)——条件渲染、列表渲染、事件处理器
一.条件渲染 1.1.v-if 在字符串模板中,如 Handlebars ,我们得像这样写一个条件块: <!-- Handlebars 模板 --> {{#if ok}} <h1&g ...
- vue2.0 — 移动端的输入框实时检索更新列表
我们都是行走在这世界的孤独者 - 暖暖 最近在做vue2.0的项目遇到一个移动端实事检索搜索更新列表的效果,但用户在搜索框输入客户的电话或姓名的时候,客户列表内容会做相应的更新,下面给大家看下图~· ...
- vue2.0的虚拟DOM渲染
1.为什么需要虚拟DOM 前面我们从零开始写了一个简单的类Vue框架(文章链接),其中的模板解析和渲染是通过Compile函数来完成的,采用了文档碎片代替了直接对页面中DOM元素的操作,在完成数据的更 ...
- 从壹开始前后端分离 [ Vue2.0+.NET Core2.1] 二十五║初探SSR服务端渲染(个人博客二)
缘起 时间真快,现在已经是这个系列教程的下半部 Vue 第 12 篇了,昨天我也简单思考了下,可能明天再来一篇,Vue 就基本告一段落了,因为什么呢,这里给大家说个题外话,当时写博文的时候,只是想给大 ...
- 【vue2】Style和Class,条件,列表渲染,双向数据绑定,事件处理
目录 1.style和class 2. 条件渲染 2.1 指令 2.2 案例 3. 列表渲染 3.1 v-for:放在标签上,可以循环显示多个此标签 3.2 v-for 循环数组,循环字符串,数字,对 ...
随机推荐
- 《Linux内核设计》第17章学习笔记
- vue props 用法(转载)
前面的话 组件接受的选项大部分与Vue实例一样,而选项props是组件中非常重要的一个选项.在 Vue 中,父子组件的关系可以总结为 props down, events up.父组件通过 props ...
- PAT 1056 组合数的和
https://pintia.cn/problem-sets/994805260223102976/problems/994805271455449088 给定 N 个非 0 的个位数字,用其中任意 ...
- IE下JS保存图片
function ieSave() { var img = document.images[0]; ...
- [转自知乎]飞腾国产CPU的部分知识
1. 作者:常成链接:https://www.zhihu.com/question/48948852/answer/113595308来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...
- MySQL分区和分表
一.概念 1.为什么要分表和分区?日常开发中我们经常会遇到大表的情况,所谓的大表是指存储了百万级乃至千万级条记录的表.这样的表过于庞大,导致数据库在查询和插入的时候耗时太长,性能低下,如果涉及联合查询 ...
- Linux、Debian、Jenkins、GIT、Nginx、码云安装,自动化部署前后端分离项目
1.安装Jenkins i:下载Jenkins安装包(war文件):https://jenkins.io/download/ ii:这里采用Tomcat的war包方式安装,讲下载好的安装包放到Tomc ...
- js 錯誤
try{ //需要被檢測是否拋出錯誤 } catch(err) { //錯誤處理代碼 } try.catch成對出現 throw:拋出錯誤 當錯誤發生時,javascript引擎停止運行,并生成一個錯 ...
- ELK--filebeat命令行参数解释
./filebeat 使用-c命令行上的标志设置要加载的配置文件,可以通过重复-c标志来指定多个配置文件, 可以使用覆盖个别设置-E <setting>=<value>.< ...
- 快乐的Lambda表达式(一)
转载:http://www.cnblogs.com/jesse2013/p/happylambda.html 原文出处: Florian Rappl 译文出处:Jesse Liu 自从Lambda ...