1. 概述

老话说的好:单打独斗是不行的,要懂得合作。

言归正传,今天我们来聊聊 VUE3 的 循环渲染。

2. 循环渲染

2.1 循环渲染数组

<body>
<div id="myDiv"></div>
</body>
<script>
const app = Vue.createApp({ // 创建一个vue应用实例
data() {
return {
myList:["apple", "pear", "orange"]
}
},
template : `
<div>
<div v-for="item in myList">{{item}}</div>
</div>
`
}); // vm 就是vue应用的根组件
const vm = app.mount('#myDiv'); // 绑定id为 myDiv 的元素

2.2 循环数组得到元素和下标

        template : `
<div>
<div v-for="(item, index) in myList">
{{index}} - {{item}}
</div>
</div>
`

2.3 遍历对象

        data() {
return {
myObject:{
"firstFruit": "apple",
"secondFruit": "pear",
"thirdFruit": "orange"
}
}
}, template : `
<div>
<div v-for="(value, key, index) in myObject">
{{index}} - {{key}} - {{value}}
</div>
</div>
`

遍历对象可以得到 下标、key 和 value

2.4 数组中追加元素

        data() {
return {
myList:["apple", "pear", "orange"]
}
},
methods : {
addElementToList() {
this.myList.push("newFruit");
}
},
template : `
<div>
<div v-for="(item, index) in myList">
{{index}} - {{item}}
</div>
<button @click="addElementToList">新增</button>
</div>
`

2.5 数组中从头部插入元素

        methods : {
addElementToListHead() {
this.myList.unshift("newFruit");
},
},
template : `
<div>
<div v-for="(item, index) in myList" >
{{index}} - {{item}}
</div>
<button @click="addElementToListHead">从头部新增</button><br>
</div>
`

2.6 从数组尾部删除元素

        methods : {
popElementFormList() {
this.myList.pop();
},
},
template : `
<div>
<div v-for="(item, index) in myList" >
{{index}} - {{item}}
</div>
<button @click="popElementFormList">从尾部删除</button>
</div>
`

2.7 从数组头部删除元素

        methods : {
shiftElementFormList() {
this.myList.shift();
},
},
template : `
<div>
<div v-for="(item, index) in myList">
{{index}} - {{item}}
</div>
<button @click="shiftElementFormList">从头部删除</button><br>
</div>
`

2.8 替换整个数组

        methods : {
replaceList() {
this.myList = ["banana", "peach"];
},
},

替换数组后,页面会重新渲染新的数组

2.9 修改数组元素

        methods : {
modifyListElement() {
this.myList[1] = "pear1";
},
},

2.10 新增对象属性

        data() {
return {
myObject:{
"firstFruit": "apple",
"secondFruit": "pear",
"thirdFruit": "orange"
}
}
},
methods : {
addAttributeToObject() {
this.myObject.fourthFruit = "banana";
}
},
template : `
<div>
<div v-for="(value, key, index) in myObject">
{{index}} - {{key}} - {{value}}
</div>
<button @click="addAttributeToObject">新增</button><br>
</div>
`

3. 综述

今天聊了一下 VUE3 的 循环渲染,希望可以对大家的工作有所帮助

欢迎帮忙点赞、评论、转发、加关注 :)

关注追风人聊Java,每天更新Java干货。

4. 个人公众号

追风人聊Java,欢迎大家关注

最新文章

  1. Java selenium web页面的滚动条操作
  2. iOS UIWebView重定向Cookie
  3. 邻接矩阵有向图(一)之 C语言详解
  4. 关于调试日志Log
  5. MATLAB 图像操作基础
  6. 在 Visual Studio 2013 中创建 ASP.NET Web 项目(0):专题导航 [持续更新中]
  7. 常见的Unix指令
  8. PND_白盾
  9. JNI(5)The Invocation API
  10. php学习笔记(1)
  11. 使用firbug调试程序写更高质量的代码设置方法
  12. for计算100以内的偶数和
  13. js 添加事件 attachEvent 和 addEventListener 的区别
  14. 转载:解决微信OAuth2.0网页授权回调域名只能设置一个的问题
  15. R语言-逻辑回归建模
  16. luogu P2520 [HAOI2011]向量
  17. 学生成绩管理系统(C++指针、链表、文件及面向对象的运用)
  18. 火币网API文档——WebSocket API Reference
  19. HBase中无法使用backspace删除
  20. August 10th 2017 Week 32nd Thursday

热门文章

  1. echarts饼图样式
  2. my41_主从延迟大排查
  3. t01_docker安装TiDB
  4. 使用beanFactory工厂实例化容器的方式实现单例模式
  5. MyBatis通过注解实现映射中的嵌套语句和嵌套结果
  6. 在html页面通过绝对地址显示图片
  7. Anaconda+pycharm(jupyter lab)搭建环境
  8. 使用Modbus批量读取寄存器地址
  9. C++内存管理:new / delete 和 cookie
  10. QPS和TPS的区别于理解