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

2018-05-15 18:20:55 +08:00
 icemanpro

想生成如下 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>
2516 次点击
所在节点    Vue.js
5 条回复
ss098
2018-05-15 18:30:38 +08:00
1. 先将 lists 以两个为一组分组(例如 underscore 提供的 chunk 方法)
2. 通过 v-for 遍历输出

例子: https://jsfiddle.net/00pkbtww/
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
2018-05-15 18:39:54 +08:00
rabbbit
2018-05-15 18:44:13 +08:00
唔,我好像理解错了
rabbbit
2018-05-15 19:14:22 +08:00
一楼方法应该是最好的,我这么写估计会被人打死

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/455041

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX