icemanpro
V2EX  ›  Vue.js

在 vue 中如何每两个 div 设置一个父 div?

  •  
  •   icemanpro · May 15, 2018 · 3213 views
    This topic created in 2940 days ago, the information mentioned may be changed or developed.

    想生成如下 html

    <div>
      <div>a</div>
      <div>b</div>
    </div>
    <div>
      <div>c</div>
      <div>d</div>
    </div>
    <div>
      <div>e</div>
    </div>
    

    以下代码应该如何修改?

    <template v-for="(l,index) of lists">
         <div>l.name</div>                   
    </template>
    
    5 replies    2018-05-15 19:14:22 +08:00
    ss098
        1
    ss098  
       May 15, 2018   ❤️ 1
    1. 先将 lists 以两个为一组分组(例如 underscore 提供的 chunk 方法)
    2. 通过 v-for 遍历输出

    例子: https://jsfiddle.net/00pkbtww/
    niubee1
        2
    niubee1  
       May 15, 2018
    <template v-for="(l,index) of lists">
    <template v-if="l % 2 === 0">
    <div>
    </template>
    <div>l.name</div>
    <template v-if="l % 2 === 1">
    </div>
    </template>
    </template>
    rabbbit
        3
    rabbbit  
       May 15, 2018
    rabbbit
        4
    rabbbit  
       May 15, 2018
    唔,我好像理解错了
    rabbbit
        5
    rabbbit  
       May 15, 2018
    一楼方法应该是最好的,我这么写估计会被人打死
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3812 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 10:24 · PVG 18:24 · LAX 03:24 · JFK 06:24
    ♥ Do have faith in what you're doing.