How to Communicate Between two Lightning Components using Events?



Event :
An event can be something the browser does or something a user does.
The lightning framework uses event-driven programming. Handlers are written for interface events and they handle the events as they fire. In Lightning, framework events are triggered from client-side JavaScript controller actions. The attributes in an event could be set before the event gets triggered and read-only when the event is handled.
In Lightning framework events are declared by the aura:event tag in a .evt resource. Events can be of two types :
  1. Component Events:
    A component event can be triggered by an instance of a component and can be handled by the component that triggered it or by any other component in the hierarchy that receive the event.
  2. Application Events:
    An application event is fired by an instance of a component and all the components that provide the handler for the event are notified.

Steps to communicate between two lightning components using events:

  1. Create an event.
  2. Create two lightning component: Child component and the parent component.
  3. Register the event in child component and fired the event in child component JavaScript controller action.
  4. Handle the event in the parent component.

A snippet of code for the lightning Event:



A snippet of code for the child component:




A snippet of code for the child component JavaScript controller : 




A snippet of code for the parent component :



A snippet of code for parent component Javascript controller :



Thanks.
Next Post Previous Post
No Comment
Add Comment
comment url