【发布时间】:2018-12-08 14:36:59
【问题描述】:
我想从一个数组中包含一个数组的道具访问数据,我必须输入大量 v-for 语句才能访问数据。
目前我必须循环遍历 spaces 数组(检查它们的 id 是否与我想要的匹配)并使用 v-if 语句只输出我想要的那个,然后我对 rooms 重复该循环(再次检查 ID)。
有没有办法通过数组中的数组filter?没有这些 for/if 语句?我一直在阅读过滤器,但无法弄清楚我需要什么。
Vue 文件:
<v-flex hidden-sm-and-down sm4 lg4 class="sidebarSticky">
<v-layout v-for="space in spaces"
v-if="space.id === selectedSpace"
:key="space.id"
>
<!-- 1 room details -->
<v-flex v-for="room in space.rooms"
v-if="selectedRoom === room.id"
v-if="selectedRoom "
:key="room.id">
<v-card>
<v-card-text>
<!-- room name -->
<h2 class="sidebarRoomName"> {{ room.name }} </h2>
<!-- description -->
<p> {{ room.description }} </p>
</v-card-text>
</v-card>
</v-flex>
</v-layout>
</v-flex>
我的数据:
new Vue({
el: '#app',
data: () => ({
selectedSpace: 0;
selectedRoom: 1,
spaces: [
{
id: 0,
name: 'House in Amsterdam',
rooms: [
{
id: 0,
name: 'My epic living room'
},
{
id: 1,
name: 'My epic room'
}
]
},
{
id: 5,
name: 'House in Paris',
rooms: [
{
id: 0,
name: 'My epic bathroom'
}
]
}
]
})
})
上面的代码可能看起来很简单,但我正在处理更多的数据,而且处理起来可能非常困难。
【问题讨论】:
标签: javascript vue.js vuejs2