用 v-for 來遍歷一個對象的 property。
- 第一個參數
value是被迭代的數組元素的別名。 - 第二個參數為
property名稱 (也就是鍵名)。 - 第三個參數作為索引。
<template>
<view>
<view v-for="(value, name, index) in object">
{{ index }}. {{ name }}: {{ value }}
</view>
</view>
</template>
<script>
export default {
data() {
return {
object: {
title: 'How to do lists in Vue',
author: 'Jane Doe',
publishedAt: '2020-04-10'
}
}
}
}
</script>
結果
0.title: How to do lists in Vue,
1.author: Jane Doe,
2.publishedAt: 2020-04-10
- 在H5平台 使用 v-for 循環整數時和其他平台存在差異,如
v-for="(item, index) in 10"中,在H5平台 item 從 1 開始,其他平台 item 從 0 開始,可使用第二個參數 index 來保持一致。 - 在非H5平台 循環對象時不支持第三個參數,如
v-for="(value, name, index) in object"中,index 參數是不支持的。 - 小程序端數據為差量更新方式,由於小程序不支持刪除對象屬性,使用的設置值為 null 的方式替代,導致遍歷時可能出現不符合預期的情況,需要自行過濾一下值為 null 的數據
