当前位置:网站首页>2.4 循环语句
2.4 循环语句
2022-07-20 21:00:00 【Qwe7】
2.4 循环语句
Vue中的循环关键字并没有Java的那么多,只有v-for,但用法上有多种。接下来我们来逐一介绍。
我们需要定义数据源,然后通过v-for来遍历数据源,再使用差值表达式输出数据。
a.普通循环
<body>
<div id="app">
<ul>
<li v-for="a in args">{{a}}</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<script>
new Vue({
el:'#app',
data:{
args:[1,2,3,4,5,6]
}
});
</script>
b.带着索引的for
<body>
<div id="app">
<ul>
<li v-for=" (a,i) in args" :key='i'>{{i}}{{a}}</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<script>
new Vue({
el:'#app',
data:{
args:[1,2,3,4,5,6]
}
});
</script>
c.遍历对象
<body>
<div id="app">
<ul>
<li v-for="(v,k,i) in student">{{i+1}}--{{k}}--{{v}}</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<script>
new Vue({
el:'#app',
data:{
student:{
username:'小鱼',
age:20,
girl:'如花'
}
}
});
</script>
v、k、i 这几个字符可以自己定义,分别表示每次循环内容的值、键、序号。
- v: 循环中每条数据的值 小鱼、20、如花
- k: 循环中每天数据的键 username、age、girl
- i: 循环的序号,从0开始
d.遍历对象数组
<body>
<div id="app">
<ul>
<li v-for=" student in students">
<span v-for="(v,k,i) in student">{{i+1}}--{{k}}--{{v}}</span>
</li>
</ul>
</div>
</body>
<script src="https://cdn.bootcss.com/vue/2.6.10/vue.js"></script>
<script>
new Vue({
el:'#app',
data:{
students:[
{
name:'xiaoming',
age:20
},
{
name:'xiaowang',
age:21
}
]
}
});
</script>
可以清楚的看到,此时数据源是一个student数组,通过两层v-for循环,外层遍历数组中的每个student对象,内层v-for遍历每个对象的v、k、i。
页面效果如下:
边栏推荐
- Section 7 of Chapter 2: the concept of list
- service和systemctl的区别/修改PATH的方法/一条命令查看IP地址和网关以及DNS服务器
- DAMA-第十二章(元数据管理)
- DAMA-第十一章(数据仓库与商务智能)
- Hutoo - date time tool -dateutil
- Research Report - visible light communication and visible light positioning
- WinForm版本更新(简易版)
- Indoor positioning of mobile robot and application of Internet of things based on visible light communication
- Opportunities and challenges coexist for financial enterprises to go to sea in emerging markets, advance AI ensures its safety and compliance development
- Web3 traffic aggregation platform starfish OS gives players a new paradigm experience of metauniverse
猜你喜欢
DAMA-第十章(参考数据与主数据)
Web3 Traffic Aggregation Platform Starfish os, Explaining Real Business' p2e 'Ecology
Spark Sql编译模块-词法、语法分析器Antlr4(一
图解LeetCode——1260. 二维网格迁移(难度:简单)
Implementation details of SAP e-commerce cloud Spartacus UI sitecontexturlparams
Automatic invoice processing - get rid of the shackles of paper and data input, automate workflow and exception handling, and significantly shorten the audit preparation time
Vulnhub靶机-doubletrouble
[daily question] 1260 2D mesh migration
LoRa基站覆盖范围
DevExpress TreeList 实现父节点列值选中,其下子节点也全部选中
随机推荐
Skillfully using roaringbitmap to deal with the memory diff problem of massive data
Section 4 of Chapter 2: replacement and cutting
[public class preview]: cloud video conference system privatization practice
Kingbasees database administrator's Guide -- 16 automatic maintenance tasks of management database
Starfish OS:以现实为纽带,打造元宇宙新范式
leetcode:407. Connected to rainwater II
DevExpress XtraReport报表开发相关知识点
DAMA-第九章(文件与内容管理)
Design details related to sap e-commerce cloud Spartacus UI store
SQL Server 查询语法汇总
第二章 第十四节:字典的概念
浅聊一下 布隆过滤器
Flask 源码剖析(一)请求入口
DevExpress之GridControl按delete键删除所选行项
Spark Sql编译模块-词法、语法分析器Antlr4(一
数据仓库和数据中台的关系
第二章第三节:字符串常规操作
从去IOE到CIPU,中国云计算要走出自己的路径
Lora技术助力冷链发展
第二章 第五节:查找和判断