Vue中列表渲染指令v-for的基本用法詳解
一、原理概述
v-for指令時在模板編譯的代碼生成階段實現(xiàn)的,當遍歷數(shù)組或?qū)ο髸r需要使用列表渲染指令v-for。當Vue.js用v-for正在更新已渲染過的元素列表時,它默認用"就地復用"策略。如果數(shù)據(jù)項的數(shù)據(jù)被改變,Vue.js將不再移動DOM元素來匹配數(shù)據(jù)項的改變,而是簡單復用此處每個元素,并確保它在特定索引下顯示已被渲染過的每個元素。
二、基本用法
v-for是Vue.js的循環(huán)語句,它的表達式需要結(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程序設計","android程序設計","php程序設計","呵呵呵"], }, methods: { } }) </script> </body> </html>
執(zhí)行結(jié)果:
在表達式中,lists是數(shù)組,item是當前一條數(shù)據(jù),index代表當前索引值。列表渲染也可以用in來代替of作為分隔符。代碼中還有一個key屬性,key屬性可以提高循環(huán)的性能。
(2)v-for循環(huán)對象
示例:
<!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遍歷對象</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)對象數(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 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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
關(guān)于Vue?CLI3中啟動cli服務參數(shù)說明
這篇文章主要介紹了關(guān)于Vue?CLI3中啟動cli服務參數(shù)說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04vue3父組件異步props傳值子組件接收不到值問題解決辦法
這篇文章主要給大家介紹了關(guān)于vue3父組件異步props傳值子組件接收不到值問題的解決辦法,需要的朋友可以參考下2024-01-01vue.js實現(xiàn)開關(guān)(switch)組件實例代碼
這篇文章介紹了vue.js實現(xiàn)開關(guān)(switch)組件的實例代碼,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06關(guān)于vue.extend和vue.component的區(qū)別淺析
最近工作中遇到了vue.extend,vue.component,但二者之間的區(qū)別與聯(lián)系是什么呢?下面這篇文章主要給大家介紹了關(guān)于vue.extend和vue.component區(qū)別的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-08-08