5

I'm trying to use the new lightning:isUrlAddressable interface to pass a few variables into a lightning component that is inside a lightning tab. In the init function I try to get the pageRefernce with the code

var myPageRef = component.get("v.pageReference");
console.log(myPageRef);

myPageRef will only return null right now and I'm not sure why. I have made sure the api version is 43. I am loading some other scripts into the page as well and I have tried getting the pageReference before and after loading the scripts. with no change.

<ltng:require styles="{!join(',', 
                            $Resource.FSL__FontAwesome44 + '/css/font-awesome.min.css')}"
              scripts="{!join(',', 
                            $Resource.jquery_3_1_0_min, 
                            $Resource.bootstrapNew + '/bootstrap-3.3.7-dist/js/moment.js',
                            $Resource.bootstrapNew + '/bootstrap-3.3.7-dist/js/bootstrap.datepicker.js')}"
              afterScriptsLoaded="{!c.jsLoaded}" />

I have also tried including

<lightning:workspaceAPI aura:id="workspace"/>
<lightning:navigation aura:id="navService"/>

but neither seems to have any effect and I don't think they should from what I know about them.

Has anyone else had any problems with this interface?

2 Answers 2

2

I had a similar issue, it constantly returns null if the component is being used on a flexipage (this isn't supported, see documentation https://developer.salesforce.com/docs/component-library/bundle/lightning:hasPageReference/documentation).

4
  • That worked, removing flexipage:availableForAllPageTypes got the pageReference to actually populate.
    – byarsawich
    Oct 1, 2018 at 17:21
  • I get an error, The component must implement at least one of flexipage:availableForAllPageTypes interface.: Source @Adam Squires .. any suggestions? Apr 25, 2019 at 19:44
  • @disasterlooms you are doing something else in your component which requires flexipage:availableForAllPageTypes (no idea without seeing your component, but start removing things and im sure you can work it out relatively quickly). You'll therefore need to chose if that feature is necessary or getting the pageref. You may be able to do both by separating into a child component. Apr 26, 2019 at 11:06
  • @adam squires figured it out. The component was on a record page layout. I had to remove it from layout and then could remove the flexipage reference. Worked like a charm.. Apr 27, 2019 at 18:59
1

I've found some issues with caching of the PageReference object that have been solved by using a handler - they might help you too.

I would run the init routine first on script load, with a check for the validity of the PageReference object. ie:

var pageReference = component.get('v.pageReference');
console.log(JSON.stringify(pageReference));
if ($A.util.isEmpty(pageReference) || $A.util.isEmpty(pageReference.state) || $A.util.isEmpty(pageReference.state.id)) {
  return;
}
var recordId = pageReference.state.id;

The init routine will abort if the PageReference is null or has no params.

Then, use a handler to listen for changes to the pageReference attribute. It seems that it gets refreshed AFTER the initial load of the component, which could be causing all your problems:

<aura:handler name="change" value="{!v.pageReference}" action="{!c.init}" />

You may want to pull the parsing of the url params into a separate routine or use a flag to indicate that the component has loaded if you want to avoid running the init routine twice.

1
  • 1
    Tried this out and it seems like it should work, but the handler for the v.pageReference never actually fires. It seems like it is staying as null for some reason. <aura:component controller="WorkOrderLightningController" implements="lightning:isUrlAddressable,flexipage:availableForAllPageTypes" access="global"> That's the header on my lightning component right now, which should be all I need to get the pageReference variable right?
    – byarsawich
    Sep 6, 2018 at 14:13

You must log in to answer this question.

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