v-for(數組)
<template> <p id="myapp"> <!--普通--> <ul> <li v-for="item in list"> {{item.name}} - {{item.price}} </li> </ul> <hr> <!--v-text--> <ul> <li v-for="item in list" v-text="item.name + ' - ' + item.price"></li> </ul> <hr> <!--帶序號 并且給奇數行添加一個class=add--> <ul> <li v-for="(item,index) in list" :class="{add:index % 2}"> {{item.name}} - {{item.price}} - {{index}} </li> </ul> </p></template><script> export default { data: function () { return { list: [ { name: 'apple', price: 34 }, { name: 'banana', price: 56 } ] } } }</script>
執行結果:
v-for(對象) 獲取key - value
<template> <p id="myapp"> <!--v-for 對象--> <!--只獲取value--> <ul> <li v-for="value in objList"> {{value}} </li> </ul> <!--獲取key -value--> <ul> <li v-for="(value, key) in objList"> {{key}} - {{value}} </li> </ul> </p></template><script> export default { data: function () { return { objList: { name: 'apple', price: 34, color: 'red', weight: 14 } } } }</script>
執行結果:
v-for(子組件)
先創建一個a組件
代碼a.vue代碼如下:
<template> <p class="hello"> {{ hello }} </p></template><script> export default { data () { return { hello: 'I am componnet a' } } }</script>
在MyApp.vue中調用
<template> <p id="myapp"> <componentA v-for="(value, key) in objList"></componentA> </p></template><script> import componentA from './components/a.vue' export default {// 注冊組件 components: {componentA}, data: function () { return { objList: { name: 'apple', price: 34, color: 'red', weight: 14 } } } }</script>
執行結果:
相信看了本文案例你已經掌握了方法,更多精彩請關注Gxl網其它相關文章!
推薦閱讀:
使用Vue.js有哪些注意事項
深入JavaScript之DOM的高級應用
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com