0

how can i pass the data from child window to parent window using lightning web component

  • to populate the new window i used this code popup = window.open("fileName", "Popup", "width=600,height=700"); popup.focus();

2 Answers 2

1

Use the Lightning Message Service to communicate between windows.

Source Example:

// myPublisherComponent.js
import { LightningElement } from 'lwc';
import { createMessageContext, releaseMessageContext,
         publish } from 'lightning/messageService';
import SAMPLEMC from "@salesforce/messageChannel/SampleMessageChannel__c";
 
export default class MyPublisherComponent extends LightningElement {
    context = createMessageContext();
     
    publishMC() {
        const message = {
            recordId: "some string",
            recordData: { value: "some value" }
        };
        publish(this.context, SAMPLEMC, message);
    }
     
    disconnectedCallback() {
        releaseMessageContext(this.context);
    }
}

Destination Example:

import { LightningElement, track } from 'lwc';
import { createMessageContext, releaseMessageContext
         subscribe, unsubscribe } from 'lightning/messageService';
 
import SAMPLEMC from "@salesforce/messageChannel/SampleMessageChannel__c";
 
export default class MySubscriberComponent extends LightningElement {
    context = createMessageContext();
    subscription = null;
    @track receivedMessage = '';
   
    subscribeMC() {
       if (this.subscription) {
           return;
       }
       this.subscription = subscribe(this.context, SAMPLEMC, (message) => {
           this.handleMessage(message);
       });
    }
   
    unsubscribeMC() {
        unsubscribe(this.subscription);
        this.subscription = null;
    }
 
    handleMessage(message) {
        this.receivedMessage = message ? JSON.stringify(message, null, '\t') : 'no message payload';
    }
 
    disconnectedCallback() {
        releaseMessageContext(this.context);
    }
}
0

Create custom events and dispatch them from the child component to parent component. Refer to this link: https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.events_create_dispatch

1
  • Events don't work across windows, except for a few special cases, such as the message event, and even then, you wouldn't construct an eent for that.
    – sfdcfox
    Commented Apr 24, 2020 at 17:30

You must log in to answer this question.

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