Search code examples
javascriptvue.jsevents

click function is a property of an object in emit event


I came across the following code and it obviously emit notification-alert event and an object as a parameter

       this.$root.$emit('notification-alert', {
          text,
          type: 'warning',
          click: () => this.unselect(file),
        });

What I don't understand is this line click: () => this.unselect(file)

  • How can a click function be considered as a property of that object?
  • What does that mean and how can that function be used afterwards?

Solution

  • click is a callback that is passed as a property of event object to the place where this event is listened, where it can be called:

       this.$root.$on('notification-alert', e => {
         // calls this.unselect(file) in the context where click function was defined
         e.click();
       });