1

I am trying to pass data from lwc to aura via LMS. How can i do it? I dont really understand what to do in aura.

handleReceiveMessage executed, but i cannot get data.

LWC

import { LightningElement, wire } from 'lwc';
import messageChannel from '@salesforce/messageChannel/SampleMessageChannel__c';
import {publish, MessageContext} from 'lightning/messageService'

export default class Publisher extends LightningElement {

    @wire(MessageContext)
    messageContext;

    handleButtonClick(event) {
        let message = {messageText: 'This is a test'};
        publish(this.messageContext, messageChannel, message);
    }
}

Aura

    <aura:component access="global" implements="lightning:availableForFlowScreens">
    
    <aura:attribute  name="messageText" type="String" />
      <lightning:messageChannel type="SampleMessageChannel__c"
                              aura:id="SMC" 
                              scope="APPLICATION"
                              onMessage="{!c.handleReceiveMessage}"
                         />
    <div>{!messageText}</div>
    </aura:component>



    ({
        handleReceiveMessage: function (component, event, helper) {
                if (event != null) {
                    const message = event.getParam('messageText');
                    component.set("v.messageText", 'Message: ' + message );
                }
            }
    });
1
  • Hint: If you put three tickmarks ( ``` ) just above the beginning of a code block and three tickmarks just below the end of a code block in your Stack Exchange questions and answers, it will make it easier to format the code to make it look like you want (and easier to read). You then do not have to rely on indenting the first line.
    – Moonpie
    Sep 24, 2021 at 16:33

1 Answer 1

0

You need to specify the correct scope in order to receive the message:

<lightning:messageChannel type="SampleMessageChannel__c"
                          aura:id="SMC" 
                          scope="APPLICATION"
                          onMessage="{!c.handleReceiveMessage}"
                     />
1
  • I have tried but still nothing.
    – Terdivi
    Sep 24, 2021 at 15:39

You must log in to answer this question.

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