Vue中列表渲染指令v-for的基本用法詳解
一、原理概述
v-for指令時(shí)在模板編譯的代碼生成階段實(shí)現(xiàn)的,當(dāng)遍歷數(shù)組或?qū)ο髸r(shí)需要使用列表渲染指令v-for。當(dāng)Vue.js用v-for正在更新已渲染過的元素列表時(shí),它默認(rèn)用"就地復(fù)用"策略。如果數(shù)據(jù)項(xiàng)的數(shù)據(jù)被改變,Vue.js將不再移動(dòng)DOM元素來匹配數(shù)據(jù)項(xiàng)的改變,而是簡(jiǎn)單復(fù)用此處每個(gè)元素,并確保它在特定索引下顯示已被渲染過的每個(gè)元素。
二、基本用法
v-for是Vue.js的循環(huán)語句,它的表達(dá)式需要結(jié)合著in或者of來使用,類似item in items的形式。
(1)v-for循環(huán)普通數(shù)組
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../vue-2.7.14.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
#root {
width: 800px;
height: 600px;
background-color: yellowgreen;
margin: 0 auto;
text-align: center;
padding: 30px;
}
.basic {
margin: 0 auto;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="root">
<h2>v-for遍歷數(shù)組</h2>
<div class="basic">
<p v-for="(item,index) in lists" :key="index">
{{index}}------{{item}}
</p>
</div>
</div>
<script>
const vm = new Vue({
el: '#root',
data: {
lists:["java程序設(shè)計(jì)","android程序設(shè)計(jì)","php程序設(shè)計(jì)","呵呵呵"],
},
methods: {
}
})
</script>
</body>
</html>執(zhí)行結(jié)果:

在表達(dá)式中,lists是數(shù)組,item是當(dāng)前一條數(shù)據(jù),index代表當(dāng)前索引值。列表渲染也可以用in來代替of作為分隔符。代碼中還有一個(gè)key屬性,key屬性可以提高循環(huán)的性能。
(2)v-for循環(huán)對(duì)象
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../vue-2.7.14.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
#root {
width: 800px;
height: 600px;
background-color: yellowgreen;
margin: 0 auto;
text-align: center;
padding: 30px;
}
.basic {
margin: 0 auto;
border: 1px solid black;
line-height: 30px;
}
</style>
</head>
<body>
<div id="root">
<h2>v-for遍歷對(duì)象</h2>
<div class="basic">
<p v-for="(value,name,index) in car">
{{index}}-----{{name}}------{{value}}
</p>
</div>
</div>
<script>
const vm = new Vue({
el: '#root',
data: {
car: {
name: "奧迪a8",
color: "黑色",
Number: "124215dhsdhsdf"
}
},
methods: {
}
})
</script>
</body>
</html>執(zhí)行結(jié)果:

(3)v-for循環(huán)對(duì)象數(shù)組
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../vue-2.7.14.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
#root {
width: 800px;
height: 600px;
background-color: yellowgreen;
margin: 0 auto;
text-align: center;
padding: 30px;
}
.basic {
margin: 0 auto;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="root">
<h2>v-for遍歷對(duì)象數(shù)組</h2>
<div class="basic">
<p v-for="(item,index) in persons">
{{index}}-----{{item.id}}-----{{item.name}}-----{{item.age}}
</p>
</div>
</div>
<script>
const vm = new Vue({
el: '#root',
data: {
persons: [
{ id: "0001", name: "張三", age: "18" },
{ id: "0002", name: "李四", age: "18" },
{ id: "0003", name: "王五", age: "28" }
]
},
methods: {
}
})
</script>
</body>
</html>執(zhí)行結(jié)果:

(4)v-for迭代整數(shù)
示例:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="../../vue-2.7.14.js"></script>
<style>
* {
margin: 0;
padding: 0;
}
#root {
width: 800px;
height: 600px;
background-color: yellowgreen;
margin: 0 auto;
text-align: center;
padding: 30px;
}
.basic {
margin: 0 auto;
border: 1px solid black;
}
</style>
</head>
<body>
<div id="root">
<h2>v-for迭代整數(shù)</h2>
<div class="basic">
<p v-for="count of 10">
{{count}}
</p>
</div>
</div>
<script>
const vm = new Vue({
el: '#root',
})
</script>
</body>
</html>執(zhí)行結(jié)果:

到此這篇關(guān)于Vue中列表渲染指令v-for的基本用法詳解的文章就介紹到這了,更多相關(guān)Vue列表渲染指令v-for內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 淺談vue.js中v-for循環(huán)渲染
- vue.js基于v-for實(shí)現(xiàn)批量渲染 Json數(shù)組對(duì)象列表數(shù)據(jù)示例
- 詳解vuejs之v-for列表渲染
- Vue表單綁定的實(shí)例代碼(單選按鈕,選擇框(單選時(shí),多選時(shí),用 v-for 渲染的動(dòng)態(tài)選項(xiàng))
- 深入淺析Vue.js 中的 v-for 列表渲染指令
- vue中v-for指令完成列表渲染
- vue?this.$refs.xxx獲取dom注意事項(xiàng)?v-if?v-for渲染的dom不能直接使用
- Vue使用v-for數(shù)據(jù)渲染順序混亂的原因及解決方案
- Vue列表渲染v-for的使用案例詳解
- Vue列表渲染v-for用法完全解析
相關(guān)文章
vue2項(xiàng)目如何將webpack遷移為vite并使用svg解決所有bug問題
這篇文章主要介紹了vue2項(xiàng)目如何將webpack遷移為vite并使用svg解決所有bug問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
Vue keep-alive實(shí)踐總結(jié)(推薦)
本篇文章主要介紹了Vue keep-alive實(shí)踐總結(jié)(推薦),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
使用Vue 實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼功能
本文章主要來介紹一下第一個(gè)階段,也就是前端校驗(yàn)的驗(yàn)證碼的實(shí)現(xiàn),下面來介紹一下拖動(dòng)驗(yàn)證碼的具體實(shí)現(xiàn)。這篇文章主要介紹了利用 Vue 實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼,需要的朋友可以參考下2019-06-06
Vue3使用hooks解決字典數(shù)據(jù)的顯示問題
我們?cè)谑褂?nbsp;element-plus的時(shí)候,經(jīng)常會(huì)使用一些字典數(shù)據(jù), 在搜索框的時(shí)候,字典數(shù)數(shù)要使用 el-select el-option 來顯示,但是經(jīng)常會(huì)遇到字典數(shù)據(jù)的顯示問題,所以本文給大家介紹了Vue3使用hooks解決字典數(shù)據(jù)的顯示問題,需要的朋友可以參考下2024-12-12
vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能
這篇文章主要為大家詳細(xì)介紹了vue組件實(shí)現(xiàn)發(fā)表評(píng)論功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Nginx配置Vue項(xiàng)目,無法按路徑跳轉(zhuǎn)及刷新404的解決方案
這篇文章主要介紹了Nginx配置Vue項(xiàng)目,無法按路徑跳轉(zhuǎn)及刷新404的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Vue3獲取和操作DOM元素的項(xiàng)目實(shí)踐
在Vue3中,有時(shí)我們需要直接操作DOM節(jié)點(diǎn),本文主要介紹了Vue3獲取和操作DOM元素的項(xiàng)目實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
關(guān)于uniapp的高級(jí)表單組件mosowe-form
這篇文章主要介紹了關(guān)于uniapp的高級(jí)表單組件mosowe-form,由于一些表單標(biāo)簽改來改去比較繁瑣,重復(fù)性很多,且樣式布局啥的幾乎萬變不離其中,為了偷懶,開發(fā)了mosowe-form及mosowe-table兩款高級(jí)組件,需要的朋友可以參考下2023-04-04

