1π
β
Yes, just add v-if
for each tab content(add wrapper because v-for
has higher priority over v-if
, added wrapper):
<button v-for="tab in tabs"
v-bind:key="tab"
v-bind:class="['tab-button', { active: currentTab === tab }]"
v-on:click="currentTab = tab">
{{ tab }}
</button>
<div v-if="currentTab === tabs[0]">
<div v-for="(fruit, index) in fruits" :key="index">
<p>{{ fruit.name }}</p>
<p >{{ fruit.price }}</p>
</div>
</div>
<div v-if="currentTab === tabs[1]">
<div v-for="(vege, vege) in veges" :key="vege">
<p>{{ vege.name }}</p>
<p>{{ vege.price }}</p>
</div>
</div>
P.S. I recommend to use object instead of array, to use tabs like this v-if="currentTab === tabs.fruits"
;
π€Max Sinev
0π
try this:
<div v-if="currentTab === 'fruits'" v-for="(fruit, fruit) in fruits" :key="fruit">
<p>{{ fruit.name }}</p>
<p >{{ fruit.price }}</p>
</div>
<div v-if="currentTab === 'veges'" v-for="(vege, vege) in veges" :key="vege">
<p>{{ vege.name }}</p>
<p>{{ vege.price }}</p>
</div>
π€Mohit Gupta
0π
Like this with v-if
inside template (if you want to toggle more than one element according to Vue.js Docs)
<template v-if="currentTab === 'fruits'">
<div v-for="(fruit, index) in fruits" :key="index">
<p>{{ fruit.name }}</p>
<p >{{ fruit.price }}</p>
</div>
</template>
<template v-else>
<div v-for="(vege, index) in veges" :key="index">
<p>{{ vege.name }}</p>
<p>{{ vege.price }}</p>
</div>
</template>
π€gil
Source:stackexchange.com