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}

  • Are you trying in lightning application (.app) ? – brahmaji tammana May 28 '17 at 12:17
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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.