6

I created a lightning component that basically needs to receive an Account Id to work with, so when I was developing it, I created a standalone application and simply passed the accountId that I was using like this :

https://xxxxx.lightning.force.com/c/MyAwesomeApp.app?accountId=123456789

So now I need to put him inside a Lightning Tab, but I couln't find a way to pass my parameter there. After some search I found this code, but I think that this shouldn't be the way to handle it.

The url will be something like :

https://xxxxx.lightning.force.com/one/one.app?123456789amp;source=aloha#/n/MyLightningTab

(Where '123456789' is the ID that I need)

Then inside the 'doInit' (javascript controller), you need to get the parameter from the URL like this :

// This one gets you : '123456789amp;'     
var sPageURL = decodeURIComponent(window.location.search.substring(1));
// This one gets you : '123456789'    
var tempVer=sPageURL.split('amp;');

(The topic where I found this code is this one)

Ok, fine, that works for now, but what if I need to pass more parameters in the future? How do I send parameters to my components that are inside a Lightning Tab?

Thanks in advance.

4
  • 1
    AFAIK lightning does not provide support for this implementation, URL hacking is not allowed ( I would happy to be proven wrong). If you want to pass a parameter to your component from another component there are many ways like using aura:attribute/ aura:method or worst case go for window.postmethod to interact from 1 dom to another. Other members may also not recommend you going through URL hack route.
    – Swathi
    Sep 19 '17 at 20:49
  • Hi @Swathi, I know that is not the correct way to handle it, but I don't have other lightning component on my scenario, so I can't use events and that kind of stuff. Still, I need to find a way to handle this...
    – msonza
    Sep 19 '17 at 20:53
  • where are you embedding this component?is it inside an account page record? or somewhere like a lightning tab?
    – Swathi
    Sep 19 '17 at 20:54
  • @Swathi It is inside a lightning tab because I need to have the whole 'Salesforce Header' (if I can say on this way), so that's why I need it inside a tab. In order to make it work, I need to know who I am working with, that's why I need the parameter.
    – msonza
    Sep 19 '17 at 20:56
11

There is no direct support for URL parameters in Lightning for components (only apps can receive parameters). Depending on specifics, there are two possible alternatives that might work.


For components meant to be shown on a record page, use force:hasRecordId. You'll automatically have an attribute v.recordId that you can use (no need to define it).


For components that need additional parameters, you typically use force:navigateToComponent:

navigateToMyComponent : function(component, event, helper) {
    var evt = $A.get("e.force:navigateToComponent");
    evt.setParams({
        componentDef : "c:myComponent",
        componentAttributes: {
            contactName : component.get("v.contact.Name")
        }
    });
    evt.fire();
}
6
  • Hi @sfdcfox, thanks for this tips. Unfortunately I was imagining that there was no direct support for URL parameters when I tried to searched for it, but I try to handle my other components and use the force:navigateToComponent.
    – msonza
    Sep 20 '17 at 11:54
  • My team is grappling with this a bit. We have a use case where one Lightning page has to open a new window/tab and pass parameters to a Lightning component in that, but we still want the one.app container. The best I can come up with right now (2 undocumented "hacks" though) is to open a VF dispatcher page like /one/one.app#/alohaRedirect/apex/VfDispatcher?param1=foo&param2=bar and then have the VF page call SfdcApp.projectOneNavigator.fireContainerEvent('force:navigateToComponent', {componentDef : "c:myComponent", componentAttributes: {param1: "foo", param2: "bar"}}).
    – Charles T
    Nov 2 '17 at 17:25
  • If sforce.one were to start supporting navigateToComponent then we'd be down to only one hack.
    – Charles T
    Nov 2 '17 at 17:26
  • @CharlesT Yeah, I don't have a good answer to that at the moment; you might want to ask a new question and see if one of the other experts in our community can assist you.
    – sfdcfox
    Nov 2 '17 at 18:18
  • 1
    @sfdcfox force:navigateToComponent is now deprecated in deference to using lightning:navigation on a component implementing lightning:isUrlAddressable (as of Summer '18, API 43.0). See the release notes here. See Ranga's answer here for an example Jun 11 '18 at 13:42

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.