1

On button click on parent LWC component, i am calling lightning-navigation to call child component. I am passing recordID in the attributes as below -

<template>
<lightning-button label="CallChild" title="CallChild" onclick={handleClick}></lightning-button>
</template>

JS -

import { NavigationMixin } from "lightning/navigation";
     connectedCallback() {
        this.redirectchildcomponent = {
          type: "standard__component",
          attributes: {
            componentName: "c__ChildComp",
            recordId: this.recordId
          }
        };
        this[NavigationMixin.GenerateUrl](this.redirectchildcomponent).then(
          url => (this.url = url)
        );
      }

    handleClick() {
    this[NavigationMixin.Navigate](this.redirectchildcomponent);
    }

After button click i am getting the following url

https://XXXXXXXX.lightning.force.com/lightning/cmp/c__ChildComp

i am not getting recordid in the URL. And how to capture recordId in the child component to pass it Apex.

Regards

2 Answers 2

1

From docs, to Add Query Parameters, you will need to utilize state attribute here.

To add query parameters to the URL, update the PageReference.state property. The navigation service uses a PageReference to generate a URL. The key-value pairs of the state property are serialized to URL query parameters.

So your page reference declaration should look like as below (you can find more on the documentation link)

this.redirectchildcomponent = {
      type: "standard__component",
      attributes: {
        componentName: "c__ChildComp",
      },
      state: {
        c__recordId: this.recordId
      }
};

Note: I haven't tested this out myself, but based on the docs, this should work.

2
  • Thank u Jayant.. It worked.. Just figuring out how to retrieve the recordId in child component Jun 4, 2019 at 14:29
  • I would imagine having a public property named recordId in your child component should resolve it.
    – Jayant Das
    Jun 4, 2019 at 14:33
0

In lightning, I had to do this to retrieve the recordId

({
doInit : function(component, event, helper) {
    
    
    var pageReference = component.get("v.pageReference");
    console.log('pagereference = '+pageReference);
    if (pageReference){
        console.log('pageReference exists');
        console.log('pagereference.state = '+pageReference.state);
        if (pageReference.state.c__recordId){
            var coId = pageReference.state.c__recordId;
            console.log('Id in doInit of CompanyLocator ='+coId);
        }//if recordId
        
    }//pageReference

}//doInit

})

You must log in to answer this question.

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