1

我要做的是在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 警告]:在...处渲染匿名组件时出错

vue.runtime.common.js?d43f:435 TypeError: Cannot read property 'photo' of undefined at eval (eval at 167 (0.cd4853d….js:28), :95:31)...

基本上,我正在尝试对正在循环的数组元素进行条件循环。我怎么能做到这一点?

4

1 回答 1

2

只是改变:

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

到:

v-for="(pet, index) in pets"
于 2017-02-18T10:50:00.790 回答