VueJS 在数组元素上循环一个带有条件的数组(v-for 和 v-if)

VueJS looping an array with a condition over the array elements (v-for with v-if)

我想做的是在 v-for 循环中打印两列卡片元素。因此,如果索引是对,我打印数组的当前元素和一行中的下一个元素,并跳过索引为奇数的元素。

到目前为止我的代码:

  <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="(index, pet) in pets"

收件人:

v-for="(pet, index) in pets"