1
我想要做的是在v-for循環中打印兩列卡片元素。因此,如果索引是對,我將數組的當前元素打印到一行中的下一個元素,並跳過具有奇數索引的元素。VueJS在數組元素上循環一個數組(v-for with v-if)
我迄今爲止代碼:
<template>
<q-layout>
<!-- Main block start-->
<div v-if="!showBack" class="card scroll" id="cards-view">
<div class="layout-padding">
<p class="group">
<button class="primary circular fixed-bottom add-btn"><router-link to="/create" exact><i class="icon-32 text-white">add</i></router-link></button>
</p>
<div class="row content-center text-center gutter" v-for="(index, pet) in pets" v-if="index % 2 === 0">
<div class="auto ">
<div class="shadow-1">
<img class="responsive" :src="pets[index].name">
<div class="card-content text-bold">
<img class="responsive sex" :src="pets[index].sex">{{ pets[index].name }}
</div>
</div>
</div>
<div class="auto ">
<div class="shadow-1">
<img class="responsive" :src="pets[index+1].name">
<div class="card-content text-bold">
<img class="responsive sex" :src="pets[index+1].sex">{{ pets[index+1].name }}
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Man block end-->
<!--- Content -->
<router-view class="layout-view"></router-view>
</q-layout>
</template>
<script>
export default {
data() {
return {
pets: [{
name: 'Júpiter',
sex: 'statics/img/female.jpg',
photo: 'statics/img/jupiter.jpg'
},{
name: 'Ringo',
sex: 'statics/img/male.jpg',
photo: 'statics/img/ringo.jpg'
}
]
}
}
}
</script>
但我收到了一個錯誤:
[Vue warn]: Error when rendering anonymous component at...
vue.runtime.common.js?d43f:435 TypeError: Cannot read property 'photo' of undefined at eval (eval at 167 (0.cd4853d….js:28), :95:31)...
基本上,我試圖做一個循環超過的元素的條件數組我正在循環。我怎麼能做到這一點?
謝謝!我不知道我在哪裏得到了v-for的文檔,或者我是否因爲沮喪而失明 - ( – daniegarcia254
很高興爲您提供幫助,文檔非常棒。 –