Navigate One Component to Another Component in Lightning Component

Lightning Component:- Component1

<aura:component implements="force:appHostable">
<aura:attribute name="Txt" type="String" default=""/>
<div class="slds-box slds-theme_default">
This is component 1.<br/><br/>
<lightning:input type="text" value="{!v.Txt}" label="Enter Text : "/><br/><br/>
<lightning:button variant="brand" label="Navigate" onclick="{!c.navigateToCompTwo}"/>
</div>
</aura:component>
Component1Controller.js

({
navigateToCompTwo : function(component, event, helper) {
var evt = $A.get("e.force:navigateToComponent");
evt.setParams({
componentDef: "c:Component2" ,
componentAttributes :{ 
Text : component.get("v.Txt") // here,pass value to another component.
}
});
evt.fire();
}
})
Lightning Component:- Component2

<aura:component implements="force:appHostable" access="global">
<aura:attribute name="Text" type="String" />
<div class="slds-box slds-theme_default">
This is component two.<br/><br/>
Text is {!v.Text}.<br/><br/>
<lightning:button variant="brand" label="Back to Component One" onclick="{!c.navigateToCompOne}"/>
</div>
</aura:component>
Component2Controller.js

({
navigateToCompOne : function(component, event, helper) {
var txtVal=component.get("v.Text"); //get componentAttributes "Text" value from Component1.
var evt = $A.get("e.force:navigateToComponent");
evt.setParams({
componentDef : "c:Component1"
});
evt.fire();
}
})

Leave a Reply