Saturday, May 18, 2019

Component event in lightning component

Component Events are fired by the child components and handled by the parent component.
A lightning framework is an event driven architecture  i.e communication between the components takes place by using events.

In the below example we will study how to fire events from the child component and handle them in the parent component.

Component Event: (Name=CompEvent)

<aura:event type="COMPONENT" description="Event template" >
    <aura:attribute name="storeMessage" type="String"/>

Child Component: (Name=ChildComp) 

<aura:component >
    <aura:registerEvent name="regInChild" type="c:CompEvent"/>
    <aura:attribute name="textMessage" type="String" default="Part from child comp"/>
    <lightning:button label="Click To Fire" onclick="{!c.doHandleEvent}"/>

Javascript controller:

 doHandleEvent : function(component, event, helper) {
  var cmpEvnt=component.getEvent("regInChild");
            storeMessage : component.get("v.textMessage")     

Parent Component: (Name=ParentComp)

<aura:component >
    <aura:handler name="regInChild" event="c:CompEvent" action="{!c.doHandleFromChild}"/>
    <aura:attribute name="parentMessage" type="String" />

Javascript controller:

 doHandleFromChild : function(component, event, helper) {
        var valueFromEvent=event.getParam("storeMessage");

Component event in lightning

No comments:

Post a Comment