I would like to catch an event and do something when v-if toggles. A conditionally rendered element is rendering. It should be possible to get notified of this, no?
You can setup a watcher on the property that is responsible for the truthy-ness for the conditional rendering (v-if
).
html
<div id="app">
<div v-if="toggleElement">MY ELEMENT</div>
<button @click="toggleElement = !toggleElement">TOGGLE</button>
</div>
script
new Vue({
el: '#app',
data: {
toggleElement: false
},
watch: {
toggleElement(newValue){
//do something if toggleElement changes
alert("Element's v-if toggled");
}
}
})
Here is the fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With