2
navigateToMyComponent : function(component, event, helper) {
    var recId = component.get("v.recordId");
    var evt = $A.get("e.force:myComponent");
    evt.setParams({
        componentDef : "c:myComponent",
        componentAttributes: {
            recordId : recId
        }
    });
    evt.fire();
},

I am using above function to navigate one component to another but getting the below error:

This page has an error. You might just need to refresh it. Action failed: c:test$controller$navigateToMyComponent [Cannot read property 'setParams' of undefined] Failing descriptor: {c:test$controller$navigateToMyComponent}

1
  • Are you trying in lightning application (.app) ?
    – brahma
    May 28, 2017 at 12:17

2 Answers 2

2

You need to be getting a reference to the force:navigateToComponent event - and you pass it the name of the component you want to navigate to. Eg:

navigateToMyComponent : function(component, event, helper) {
    var recId = component.get("v.recordId");
    var evt = $A.get("e.force:navigateToComponent");
    evt.setParams({
        componentDef : "c:myComponent",
        componentAttributes: {
            recordId : recId
        }
    });
    evt.fire();
}

Essentially, you need to fix line 3 of your code:

var evt = $A.get("e.force:myComponent");

Should be:

var evt = $A.get("e.force:navigateToComponent");
0

May be this will help you. I have used the same in my developer org and it works fine.

https://force-base.com/2016/01/04/how-to-navigate-from-one-component-to-another-in-lightning/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .