Application Events in Salesforce Lightning

DIFFERENCE BETWEEN APPLICATION EVENTS AND COMPONENT EVENTS IN LIGHTNING
Create Lightning Event :- "SampleApplicationEvent"
<aura:event type="Application" description="Sample Application Event">
<aura:attribute name="message" type="String" />
</aura:event>
Lightning Component :- "eventComponent1"
<aura:component>
<aura:registerEvent name="SampleApplicationEvent" type="c:SampleApplicationEvent"/>
<lightning:button label="Click to fire the event" variant="brand" onclick="{!c.component1Event}"/>
</aura:component>
eventComponent1Controller.Js
({
component1Event : function(cmp, event,helper) {
//Get the event using event name.
var appEvent = $A.get("e.c:SampleApplicationEvent");
//Set event attribute value
appEvent.setParams({"message" : "Welcome "});
appEvent.fire();
}
})
Lightning Component :"eventComponent2"
<aura:component >
<aura:attribute name="eventMessage" type="String"/>
<aura:handler event="c:SampleApplicationEvent" action="{!c.component2Event}"/>
<div class="slds-m-around_xx-large">
<p>{!v.eventMessage}</p>
</div>
</aura:component>
eventComponentController.Js
({
component2Event : function(cmp, event) {
//Get the event message attribute
var message = event.getParam("message");
//Set the handler attributes based on event data
cmp.set("v.eventMessage", message + ' Bvenkat Jeewesh');
}})
Create Lightning Application:- "EventApp"
<aura:application extends="force:slds">	
<c:eventComponent1/>
<c:eventComponent2/>
</aura:application>
OutPut:

Leave a Reply