Simply put, all you need to do is set a flag for later use with the v-if
:
<div id="app">
<div v-for="item in items">
<button @click="$set(item, 'shown', true)">Show child</button>
<div>{{ item.name }}</div>
<div v-if="item.shown">Child component</div>
</div>
</div>
In this case, $set()
is necessary because the initial item
may not have the shown
field, so setting it directly as item.shown=true
won't trigger reactivity.
You can also hide the button after it's clicked:
<button @click="$set(item, 'shown', true)" v-if="!item.shown">Show child</button>
To toggle visibility, you just need to follow this pattern:
<button @click="$set(item, 'shown', !item.shown)">
{{ item.shown ? 'Hide' : 'Show' }} child
</button>
Check out JSFiddle