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>
登錄后復制