7

Other than the sparse documentation example code I couldn't find any that shows regarding lightning:navigation and lightning:isUrlAddressable.

I couldn't get this simple example to run.

navigateTo.component (API 43)

<aura:component .. implements="force:hasRecordId,lightning:isUrlAddressable">

</aura:component>

navigateFrom.component (API 43)

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
    <lightning:navigation aura:id="nav" />
</aura:component>

navigateFromController.js (API 43)

({
    foo: function(cmp, evt, helper) {
        cmp.find("nav").navigate({
            type: "standard__component",
            attributes: {
                componentName: "MYNAMESPACE__navigateTo"

            },
            state: {
                recordId: cmp.get("v.recordId")
            }
        });
    },
});

I also tried c__navigateTo instead of MYNAMESPACE__naviagteTo and moving recordId: cmp.get("v.recordId") from state to attributes but in both no navigation takes place.

I am looking for an elegant solution where I DON'T have to touch the way how components get and set the v.recordId as describe in the solution by Mark Masterson.

0

2 Answers 2

3

The navigation system only passes information from the URL into a component through the v.pageReference attribute, it will not automatically detect and use any other interfaces, like force:hasRecordId. So you will have to grab the recordId from v.pageReference.state.*

1
  • Is this just a guess or is there any official document stating this? I am just asking because this is really very unelegant. Every component that is using force:hasRecord AND lightning:navigation needs to check two places for the record it. Ugly! Commented Jun 11, 2018 at 7:32
1

I was able to get the Navigation to work by doing this:

navigateTo.component (API 43)

<aura:component .. implements="lightning:isUrlAddressable">
    <lightning:card iconName="standard:event" title="NavigateTo">
        <lightning:formattedText class="slds-p-left_small"
                    value="{!v.pageReference.state.recordId}"/>
    </lightning:card>
</aura:component>

navigateFrom.component (API 43)

<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId">
    <lightning:navigation aura:id="nav" />
    <lightning:button label="Navigate" onclick="{!c.foo}"/>
</aura:component>

navigateFromController.js (API 43)

({
    foo: function(cmp, evt, helper) {
        cmp.find("nav").navigate({
            type: "standard__component",
            attributes: {
                componentName: "c__navigateTo"

            },
            state: {
                recordId: cmp.get("v.recordId")
            }
        });
    },
});
3
  • 1
    Also, I found this documentation in the component library to be a bit better: developer.salesforce.com/docs/component-library/bundle/… Commented Jun 6, 2018 at 18:25
  • 1
    Thanks Mark for taking your time. But there is no way I can change remove the force:hasRecordId from the navigateTo components implement. That's how this component works. My question is about how to call an existing component that is not expecting an arbitrary attribute which is accidentially called recordId but the Aura one. Commented Jun 6, 2018 at 18:31
  • 1
    Setting the recordId attribute in the NavigateTo's init handler won't help (just throwing out ideas). It doesn't appear that there is a way to directly set attributes when using lightning:navigation at this time without setting them from the pageReference. Commented Jun 6, 2018 at 19:32

You must log in to answer this question.

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