How to Communicate Between two Lightning Components using Events?
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 :
- 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.
- 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:
- Create an event.
- Create two lightning component: Child component and the parent component.
- Register the event in child component and fired the event in child component JavaScript controller action.
- 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.