Is it possible to react to an event within a vue template? For example, if a child component dispatches an event like $dispatch('userAdded')
, can I display a message in the parent component based on that?
<div class="alert alert-info" v-if="userAdded">
User was created!
</div>
If not, is there a way to access variables from the child component instead?
<div class="alert alert-info" v-if="$refs.addView.form.successful">
User was created!
</div>
I've tried both approaches without success.
Also, while we're on the subject, is there a clean way to automatically hide elements after a certain amount of time? Perhaps something like this (to hide after 2 seconds):
<div class="alert alert-info" v-if="$refs.addView.form.successful" hide-after="2000">
User was created!
</div>
Thank you!
edit: I've created my own hide-after
directive:
Vue.directive('hide-after', {
update: function(value) {
setTimeout(() => this.el.remove(), value);
}
});
<div class="alert alert-info" v-hide-after="2000">
This will be shown for 2 seconds
</div>