V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
icemanpro
V2EX  ›  Vue.js

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

  •  
  •   icemanpro · 2018-05-15 18:20:55 +08:00 · 2493 次点击
    这是一个创建于 2145 天前的主题,其中的信息可能已经有所发展或是发生改变。

    想生成如下 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 条回复    2018-05-15 19:14:22 +08:00
    ss098
        1
    ss098  
       2018-05-15 18:30:38 +08:00   ❤️ 1
    1. 先将 lists 以两个为一组分组(例如 underscore 提供的 chunk 方法)
    2. 通过 v-for 遍历输出

    例子: https://jsfiddle.net/00pkbtww/
    niubee1
        2
    niubee1  
       2018-05-15 18:35:07 +08:00
    <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  
       2018-05-15 18:39:54 +08:00
    rabbbit
        4
    rabbbit  
       2018-05-15 18:44:13 +08:00
    唔,我好像理解错了
    rabbbit
        5
    rabbbit  
       2018-05-15 19:14:22 +08:00
    一楼方法应该是最好的,我这么写估计会被人打死
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2811 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 14:41 · PVG 22:41 · LAX 07:41 · JFK 10:41
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.