konstantinbueschel
3/23/2017 - 3:09 PM

Lightning - Passing data up the component hierarchy via a component event

Lightning - Passing data up the component hierarchy via a component event

({
	fireMyComponentEvent : function(component, event, helper) {
        var myEvent = component.getEvent("myComponentEvent");
        myEvent.setParams({"param": "It works!"});
        myEvent.fire();
	}
})
<aura:component>
    <aura:registerEvent name="myComponentEvent" type="c:componentEvent"/>
    
	<lightning:button label="Fire component event" onclick="{! c.fireMyComponentEvent }" />
</aura:component>
({
    handleMyComponentEvent : function(component, event, helper) {
        var value = event.getParam("param");
        alert("Received component event with param = "+ value);
    }
})
<aura:component>
    <aura:handler name="myComponentEvent" event="c:componentEvent" action="{!c.handleMyComponentEvent}"/>

    <c:componentEventSender/>
</aura:component>
<aura:event type="COMPONENT">
    <aura:attribute name="param" type="String"/>
</aura:event>