【发布时间】:2019-11-26 12:06:15
【问题描述】:
我有一个 json 文档,我从中提取以填充表格以及工作表上的其他几个项目。在文档的同一“级别”上,我有对象和数组。我不确定如何管理在 Vue 中显示数组的子项和对象的子项。
我尝试过使用各种 if 语句分别显示对象的子级和数组的子级,但都没有成功。
由于隐私问题,我无法显示实际代码,但这里有一个过于简单的示例,说明我正在查看的数据以解决这个问题:
"Pairs": [
{
"Value": {
"Id": "123"
},
"Children": [
"Id": "111",
"String": {
"Value": "999",
"Children": [
{
"Key": "555"
},
{
"Key": "444"
},
]
}
]
},
{
"Value": {
"Id": "456"
},
"Children": [
"Id": "178",
"Strings": [
{
"Value": "845",
"Children": [
{
"Key": "079"
},
{
"Key": "106"
}
]
},
"Value": "578",
"Children": [
{
"Key": "279"
},
{
"Key": "745"
}
]
]
]
}
]
那么这是我在 Vue 前端的表格
<table>
<template v-for="Pair in Pairs">
<template v-for="Child in Pair.Children">
<template v-for="String in Child.Strings"> --- PROBLEM HERE
<tr v-for="Child in String.Children">
<td>{{Child.Key}}</td>
</tr>
</template>
</template>
</template>
</table>
从上面的例子可以看出,Pairs 数组中的第一个对象有一个名为“String”的对象,第二个有一个名为“Strings”的数组。如您所料,当我尝试访问 Vue 文档中的对象 String 或数组 Strings 时,我得到一个未定义的错误。我被困住了。
【问题讨论】: