发表于: 2020-07-14 22:27:27
1 2521
今天完成的事情:今天学了vue的几个循环的语句
明天计划的事情:继续后续的学习
遇到的问题:实际操作不多还是需要更多的练习
- 收获:
循环语句
循环使用 v-for 指令。
v-for 指令需要以 site in sites 形式的特殊语法, sites 是源数据数组并且 site 是数组元素迭代的别名。
v-for 可以绑定数据到数组来渲染一个列表:
<!DOCTYPE html><html><head><meta charset="utf-8"><title>Vue 测试实例</title><script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script></head><body><div id="app"><ol><li v-for="site in sites">{{ site.name }}</li></ol></div><script>new Vue({el: '#app',data: {sites: [{ name: 'Runoob' },{ name: 'Google' },{ name: 'Taobao' }]}})</script></body></html> - 效果
- 模板中使用 v-for:<!DOCTYPE html><html><head><meta charset="utf-8"><title>Vue 测试实例</title><script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script></head><body><div id="app"><ul><template v-for="site in sites"><li>{{ site.name }}</li><li>--------------</li></template></ul></div><script>new Vue({el: '#app',data: {sites: [{ name: 'Runoob' },{ name: 'Google' },{ name: 'Taobao' }]}})</script></body></html>
v-for 迭代对象
v-for 可以通过一个对象的属性来迭代数据:
- <!DOCTYPE html><html><head><meta charset="utf-8"><title>Vue 测试实例 </title><script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script></head><body><div id="app"><ul><li v-for="value in object">{{ value }}</li></ul></div><script>new Vue({el: '#app',data: {object: {name: '测试',url: 'http://www.ceui.com',slogan: '学的不仅是技术,更是梦想!'}}})</script></body></html>
效果
你也可以提供第二个的参数为键名:
- <!DOCTYPE html><html><head><meta charset="utf-8"><title>Vue 测试实例</title><script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script></head><body><div id="app"><ul><li v-for="(value, key) in object">{{ key }} : {{ value }}</li></ul></div><script>new Vue({el: '#app',data: {object: {name: 'ceui ',url: 'http://www.ceui.com',slogan: '学的不仅是技术,更是梦想!'}}})</script></body></html>
效果
v-for 迭代整数
v-for 也可以循环整数
<!DOCTYPE html><html><head><meta charset="utf-8"><title>Vue 测试实例 - 菜鸟教程(runoob.com)</title><script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script></head><body><div id="app"><ul><li v-for="n in 10">{{ n }}</li></ul></div><script>new Vue({el: '#app'})</script></body></html>效果
剩下的明天继续
评论