vue 中用于循环遍历数据数组或对象的指令是 v-for,语法为 。参数包括:遍历项 item、可选索引 index 和要遍历的数据 items。
Vue 中用于循环的指令
Vue 中使用 v-for 指令进行循环遍历数据数组或对象。
语法:
<code class="html"><template v-for="(item, index) in items"></template></code>
登录后复制
参数:
item:当前循环项
index:当前循环项的索引(可选)
items:要遍历的数据数组或对象
用法:
- 遍历数组:
<code class="html"><ul>
<li v-for="item in items">{{ item }}</li>
</ul></code>
登录后复制
- 遍历对象:
<code class="html"><ul>
<li v-for="(value, key) in object">{{ key }}: {{ value }}</li>
</ul></code>
登录后复制
- 循环索引:
<code class="html"><ul>
<li v-for="(item, index) in items">{{ index + 1 }}. {{ item }}</li>
</ul></code>
登录后复制






