How can I add a element everytime after 4 elements in Vue.js?

家住魔仙堡 提交于 2019-12-25 16:55:19

问题


I'm immigrating my code to Vue.js so pretty new to Vue. As you see the screenshot(link below), There are 4 columns inside div with columns class name. I was trying to use v-if to add the div(class columns) conditionally but I have no idea how to get a length of columns elements in Vue.

Example Code

<template lang="pug">
.container
  .columns(v-if="") // add this conditionally
    .column.is-3.vid(v-for='item in items')
      .panel
        p.is-marginless
         a(href=item.videoId)
           img(src=item.thumbnail)
        .panel.vidInfo
          .columns.hax-text-centered
            .column
              .panel-item.reddit-ups
                | item.score
                i.fa.fa-reddit-alien.fa-2x
              .panel-item.reddit-date
                i.fa.fa-calendar.fa-2x
</template>

回答1:


You can access v-for iteration number like this:

change

 .column.is-3.vid(v-for='item in items')

to

.column.is-3.vid(v-for='(item, index) in items')

then somewhere where you need it check the index:

v-if="(index % 4 === 0) ? 'show it after every 4 elements' : ''"

for example, inject a span:

<span v-if="index % 4 === 0">after 4</span>


来源:https://stackoverflow.com/questions/43224096/how-can-i-add-a-element-everytime-after-4-elements-in-vue-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!