Vue.js中v-for指令的用法介紹
一、什么是v-for指令
在Vue.js中,我們可以使用v-for指令基于源數(shù)據(jù)重復渲染元素。也就是說可以使用v-for指令實現(xiàn)遍歷功能,包括遍歷數(shù)組、對象、數(shù)組對象等。
二、遍歷數(shù)組
代碼示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!--引入vue.js-->
<script src="node_modules/vue/dist/vue.js" ></script>
<script>
window.onload=function(){
// 構建vue實例
new Vue({
el:"#my",// el即element,表示掛載的元素,不能掛載在HTML和body元素上面
data:{
array:[1,2,3,4],//數(shù)組
},
// 方法
methods:{
}
})
}
</script>
</head>
<body>
<div id="my">
<div>
<h1>下面的使用v-for遍歷數(shù)組</h1>
<div>
<h1>只顯示值</h1>
<ul>
<li v-for=" v in array">{{v}}</li>
</ul>
</div>
<div>
<h1>顯示值和索引</h1>
<ul>
<li v-for=" (v,index) in array">值:{{v}},對應的索引:{{index}}</li>
</ul>
</div>
</div>
</div>
</body>
</html>其中index表示數(shù)組的索引
效果如下圖所示:

注意:最新的版本中已經(jīng)移除了$index獲取數(shù)組索引的用法
三、遍歷對象
代碼示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<!--引入vue.js-->
<script src="node_modules/vue/dist/vue.js" ></script>
<script>
window.onload=function(){
// 構建vue實例
new Vue({
el:"#my",// el即element,表示掛載的元素,不能掛載在HTML和body元素上面
data:{
array:[1,2,3,4],//數(shù)組
},
// 方法
methods:{
}
})
}
</script>
</head>
<body>
<div id="my">
<div>
<h1>下面的使用v-for遍歷數(shù)組</h1>
<div>
<h1>只顯示值</h1>
<ul>
<li v-for=" v in array">{{v}}</li>
</ul>
</div>
<div>
<h1>顯示值和索引</h1>
<ul>
<li v-for=" (v,index) in array">值:{{v}},對應的索引:{{index}}</li>
</ul>
</div>
</div>
</div>
</body>
</html>效果如下圖所示:

四、遍歷數(shù)組對象
代碼示例如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>使用v-for指令遍歷數(shù)組對象</title>
<!--引入vue.js-->
<script src="node_modules/vue/dist/vue.js" ></script>
<script>
window.onload=function(){
// 構建vue實例
new Vue({
el:"#my",// el即element,表示掛載的元素,不能掛載在HTML和body元素上面
data:{
lists:[
{name:"kevin",age:23},
{name:"tom",age:25},
{name:"joy",age:28}
]
},
// 方法
methods:{
}
})
}
</script>
</head>
<body>
<div id="my">
<div>
<h1>下面的使用v-for遍歷數(shù)組對象</h1>
<div>
<h1>只顯示值</h1>
<ul>
<li v-for=" list in lists">name值:{{list.name}},age值:{{list.age}}</li>
</ul>
</div>
<div>
<h1>顯示值和鍵</h1>
<ul>
<li v-for=" (list,index) in lists">name值:{{list.name}},age值:{{list.age}}, 對應的鍵:{{index}}</li>
</ul>
</div>
</div>
</div>
</body>
</html>效果如下圖所示:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
解決vue create 創(chuàng)建項目只有兩個文件問題
這篇文章主要介紹了解決vue create 創(chuàng)建項目只有兩個文件問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Ubuntu22.04使用nginx部署vue前端項目的詳細教程
這篇文章主要給大家介紹了關于Ubuntu22.04使用nginx部署vue前端項目的詳細教程,使用nginx部署前端項目是一篇非常詳細的教程,旨在幫助初學者使用Nginx來部署前端項目,需要的朋友可以參考下2024-03-03
vue后臺項目如何使用router.addRoutes動態(tài)加入路由的思路
這篇文章主要介紹了vue后臺項目如何使用router.addRoutes動態(tài)加入路由的思路,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06
vue3使用particles插件實現(xiàn)粒子背景的方法詳解
這篇文章主要為大家詳細介紹了vue3使用particles插件實現(xiàn)粒子背景的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-03-03
Vue Router 實現(xiàn)動態(tài)路由和常見問題及解決方法
動態(tài)路由不同于常見的靜態(tài)路由,可以根據(jù)不同的「因素」而改變站點路由列表。這篇文章主要介紹了Vue Router 實現(xiàn)動態(tài)路由和常見問題解決方案,需要的朋友可以參考下2020-03-03

